Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Blog Module /

Discuss the Blog Module.

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

Post recent (TAGGED) blog posts on home page


Reply


8 Posts   3233 Views

Avatar
jloafs

Community Member, 15 Posts

24 January 2011 at 12:28pm

Has anyone any idea how to feature latest blog posts (in summarised form) in the sidebar of a homepage using only blog posts with a specific tag ie. "news"?

Avatar
Willr

Forum Moderator, 5513 Posts

26 January 2011 at 9:55pm

Have a look at tutorial 2 for a general idea for how to get a set of pages - http://doc.silverstripe.org/sapphire/en/tutorials/2-extending-a-basic-site#showing-the-latest-news-on-the-homepage.

For the blog module tags - the tags are stored in a txt field as a comma separated list so in your database query you can use the LIKE '%news%' to filter for just news.

function LatestNews($num=5) {
return DataObject::get("ArticlePage", "Tags LIKE '%news%' ", "Date DESC", "", $num);
}

Avatar
jloafs

Community Member, 15 Posts

26 January 2011 at 10:21pm

Hi Willr,

Just looking at this now.

I currently have the latest news set up as per that tutorial. If I make the suggested change in the HomePage.php file, what would I need to change in the HomePage.ss file?

I'm new so please bear with me.

Avatar
Willr

Forum Moderator, 5513 Posts

27 January 2011 at 4:31pm

You wouldn't need to change the template as all you're basically doing is filtering the database query (from the PHP) to reduce and entries you're including.

Avatar
jloafs

Community Member, 15 Posts

28 January 2011 at 10:49am

I must be doing something wrong.

In my php file I have:

function LatestNews($num=5) {
return DataObject::get("ArticlePage", "Tags LIKE '%news%' ", "Date DESC", "", $num);
}

In my .ss file I have:

<ul id="NewsList">
<% control LatestNews %>
<li class="newsDateTitle"><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></li>
<li class="newsDateTitle">$Date.Nice</li>
<li class="newsSummary typography">$Content.FirstParagraph<a href="$Link" title="Read more on &quot;{$Title}&quot;"> More...</a></li>
<% end_control %>
</ul>

But I'm getting an error and the page doesn't work at all.

Have I missed something here?

Avatar
Willr

Forum Moderator, 5513 Posts

30 January 2011 at 5:10pm

But I'm getting an error and the page doesn't work at all.

Posting the message (or a screenshot of the error) is much more useful. If you are getting a generic "Website Error" page then put your site into devmode. This enables full error messages.

function LatestNews($num=5) {
return DataObject::get("ArticlePage", "Tags LIKE '%news%' ", "Date DESC", "", $num);
}

If you are using the Blog then the PageType is "BlogEntry" not "ArticlePage".

The error page will have more useful messages.

Avatar
jloafs

Community Member, 15 Posts

31 January 2011 at 12:13am

Thanks for pointing that out.

Solved.

Much appreciated.

Avatar
juneallison

Community Member, 108 Posts

28 July 2011 at 8:32am

Hi,

I have a question that is related to this functionality. Or I guess an extension of this functionality. Even though I haven't tried it, I believe I understand how to implement what has been described in this thread if I wanted blog posts on my home page.

I am actually looking to put a few recent posts in a sidebox that will only appear on Blog Entry pages. (I think I can figure this part out). But I want the recent posts to only be posts that have the same tag as the blog entry that you are currently viewing. This is where I am getting stuck.

I am guessing it would use the first tag listed for the current blog entry. But I don't know what code/syntax is needed to grab the first tag associated with the post.

Any suggestions would be great. Thanks!