1770 Posts in 495 Topics by 531 members
Blog Module
SilverStripe Forums » Blog Module » displaying blog entry with image in homepage
Discuss the Blog Module.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 2924 Views |
-
displaying blog entry with image in homepage

15 January 2010 at 11:43pm
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 isin 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
-
Re: displaying blog entry with image in homepage

16 January 2010 at 1:40am Last edited: 16 January 2010 1:40am
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.
-
Re: displaying blog entry with image in homepage

23 June 2010 at 4:29am
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');
}}
?> -
Re: displaying blog entry with image in homepage

23 June 2010 at 4:41am
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"
); -
Re: displaying blog entry with image in homepage

23 June 2010 at 5:21am Last edited: 23 June 2010 5:22am
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.
-
Re: displaying blog entry with image in homepage

23 June 2010 at 9:09am
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. -
Re: displaying blog entry with image in homepage

23 June 2010 at 9:24am
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.
| 2924 Views | ||
|
Page:
1
|
Go to Top |




