Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » List blog posts of a specific tag on home page

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

Page: 1
Go to End
Author Topic: 895 Views
  • bostonmark
    Avatar
    Community Member
    10 Posts

    List blog posts of a specific tag on home page Link to this post

    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

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: List blog posts of a specific tag on home page Link to this post

    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 %>

  • bostonmark
    Avatar
    Community Member
    10 Posts

    Re: List blog posts of a specific tag on home page Link to this post

    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?

  • stallain
    Avatar
    Community Member
    61 Posts

    Re: List blog posts of a specific tag on home page Link to this post

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

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

    895 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.