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"?
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
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);
}
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.
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.
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 "{$Title}"">$Title</a></li>
<li class="newsDateTitle">$Date.Nice</li>
<li class="newsSummary typography">$Content.FirstParagraph<a href="$Link" title="Read more on "{$Title}""> 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?
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.
Thanks for pointing that out.
Solved.
Much appreciated.
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!