Jump to:

1840 Posts in 1563 Topics by 557 members

Blog Module

SilverStripe Forums » Blog Module » displaying blog entry with image in homepage

Discuss the Blog Module.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 3215 Views
  • Rishi
    Avatar
    Community Member
    97 Posts

    displaying blog entry with image in homepage Link to this post

    Hello
    I am displaying blog entry (last 3) in my home page .its working perfectly,
    the only problem is when I enter a Image in my blog entry then images are not shown in homepage.
    I have limited the number of word to be displayed in homepage as 10,no matter how much i increase the word count ,image is never displayed in homepage.
    Please help me out in displaying images in homepage,
    My code is

    in homepage .php

    function LatestNews($number=3) {
    $holder = DataObject::get_one('BlogHolder', "Title = 'News'");
    return DataObject::get('BlogEntry', "ParentID = {$holder->ID}","Created DESC", false, $number);
    }

    in template homepage.ss
    <% control LatestNews %>
    <p><a href="$Link">$Title</a><br>
    $Content.LimitWordCount(10) </p>
    <% end_control %>

    thank you in advance

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: displaying blog entry with image in homepage Link to this post

    I do believe LimitWordCount will do strip_tags or something to that effect so no matter how many words you get back you'll never get the image tag.

    Your best bet is probably to add an image field to the blog entry and use that instead.
    You could do something like:

    <?php
    class BlogEntryDecorator extends DataObjectDecorator {
       
       public function extraStatics(){
          return array(
             'has_one' => array(
                "TitleImage" => "Image"
             )
          );

       }   

       public function updateCMSFields(FieldSet &$fields) {
             $fields->addFieldToTab("Root.Content.Main", new ImageField("TitleImage", "Title image"), 'Content');
       }
    }
    ?>

    And then add this to _config.php:

    Object::add_extension('BlogEntry', 'BlogEntryDecorator');

    After you upload the image through the image field you can display it by writing $TitleImage in the template.

  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Re: displaying blog entry with image in homepage Link to this post

    How could I go about adding another field to this to allow the photo to have a caption? I'd assumed I could just amend it to this, but while the field appears in the CMS, when I save the page the data is lost. Thanks.

    <?php

    class BlogEntryDecorator extends DataObjectDecorator {

    public function extraStatics(){
    return array(
    'has_one' => array(
    "TitleImage" => "Image",
    "PhotoCaption" => "HTMLText"
    )
    );
    }

    public function updateCMSFields(FieldSet &$fields) {
    $fields->addFieldToTab("Root.Content.Main", new ImageField("TitleImage", "Title image"), 'Content');
    $fields->addFieldToTab("Root.Content.Main", new TextField("PhotoCaption", "Photo Caption"), 'Content');
    }

    }
    ?>

  • kidcardboard
    Avatar
    Community Member
    5 Posts

    Re: displaying blog entry with image in homepage Link to this post

    Not 100% sure but I think you might need to add your new fields to the db array also. ie

    static $db = array(
          "TitleImage" => "Image",
          "PhotoCaption" => "HTMLText"
       );

  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Re: displaying blog entry with image in homepage Link to this post

    Thanks. I tried doing that, but I wasn't sure how to it works in relation to 'return array', so couldn't get that to work.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: displaying blog entry with image in homepage Link to this post

    When you're decorating you add db fields pretty much the same way you add extra relationships through decoration. The code you have is pretty close, this should work.

    class BlogEntryDecorator extends DataObjectDecorator {

    public function extraStatics(){
    return array(
    'db' => array("PhotoCaption" => "HTMLText"),
    'has_one' => array("TitleImage" => "Image")
    );
    }

    public function updateCMSFields(FieldSet &$fields) {
    $fields->addFieldToTab("Root.Content.Main", new ImageField("TitleImage", "Title image"), 'Content');
    $fields->addFieldToTab("Root.Content.Main", new TextField("PhotoCaption", "Photo Caption"), 'Content');
    }
    }


    Try this and build the database after changing the code. If everything's setup correctly the text field should get added to the BlogEntry table.

  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Re: displaying blog entry with image in homepage Link to this post

    Thank you so much, been trying again for the last hour but not quite getting it - but this works perfectly. I'm slowly getting the hang of how these relationships work.

  • DannyC
    Avatar
    Community Member
    3 Posts

    Re: displaying blog entry with image in homepage Link to this post

    LEGEND!

    3215 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.