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.

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.

General Questions /

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

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

List blog posts of a specific tag on home page


Go to End


4 Posts   1963 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, 68 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);