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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

List blog posts of a specific tag on home page


Reply


4 Posts   950 Views

Avatar
bostonmark

Community Member, 10 Posts

2 September 2011 at 2:59pm

I have a blog module installed and I'm trying to build a small components that specifically lists blog post of a specific tag, let's call it the "home" tag. How can I accomplish this? I looked at the PHP files for the BlogPage and BlogHolder but couldn't really find syntax to "query" for these blog posts.

Thanks

Avatar
Ryan M.

Community Member, 309 Posts

3 September 2011 at 7:34pm

In your Page.php:

public function getBlogsByTag($tag) {
return DataObject::get("BlogEntry", "Tag = '{$tag}'");
}

In your HomePage.ss or Page.ss:

<% control getBlogsByTag(your-tag-name) %>
html code here...
<% end_control %>

Avatar
bostonmark

Community Member, 10 Posts

5 September 2011 at 7:04am

Edited: 05/09/2011 7:05am

Thanks Ryan. I tweaked it to use a LIKE in the SQL:

public function getBlogsByTag($tag) {
return DataObject::get("BlogEntry", "Tags LIKE '%{$tag}%'");
}

How would I go about changing this to get me only the most recent single post by a tag and how would I access that in the template?

Avatar
stallain

Community Member, 61 Posts

5 September 2011 at 10:45am

Hello, maybe you could try something like this in your function :

return DataObject::get("BlogEntry", "Tags LIKE '%{$tag}%'", Created DESC, "", 1);