Jump to:

1851 Posts in 1606 Topics by 561 members

Blog Module

SilverStripe Forums » Blog Module » Pulling SS Blog from another pages

Discuss the Blog Module.

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

Page: 1 2
Go to End
Author Topic: 3334 Views
  • Bruce B
    Avatar
    Community Member
    145 Posts

    Re: Pulling SS Blog from another pages Link to this post

    Bstarr,
    In page.php, you need to move the LatestNews function. It should sit in the page class near the top of the code. Look for a single closing bracket } around line 10 and insert it just above that.

  • Bstarr
    Avatar
    Community Member
    25 Posts

    Re: Pulling SS Blog from another pages Link to this post

    I moved it but sadly I'm still only seeing the no entries message. Any other suggestions?

    <?php
    class Page extends SiteTree {

       public static $db = array(
       );

       public static $has_one = array(
       );

    function LatestNews($num=4) {
    $news = DataObject::get_one("BlogHolder");
    return ($news) ? DataObject::get("BlogEntry", "ParentID = $news->ID", "Date DESC", "", $num) : false;
    }
    }
    class Page_Controller extends ContentController {

       /**
        * An array of actions that can be accessed via a request. Each array element should be an action name, and the
        * permissions or conditions required to allow the user to access it.
        *
        * <code>
        * array (
        * 'action', // anyone can access this action
        * 'action' => true, // same as above
        * 'action' => 'ADMIN', // you must have ADMIN permissions to access this action
        * 'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
        * );
        * </code>
        *
        * @var array
        */
       public static $allowed_actions = array (
       );

       public function init() {
          parent::init();

          // Note: you should use SS template require tags inside your templates
          // instead of putting Requirements calls here. However these are
          // included so that our older themes still work
          Requirements::themedCSS('layout');
          Requirements::themedCSS('typography');
          Requirements::themedCSS('form');
       }
    }

  • Bruce B
    Avatar
    Community Member
    145 Posts

    Re: Pulling SS Blog from another pages Link to this post

    I just noticed. In page.php, the function is called LatestNews, but in Homepage.ss its called LatestBlogPosts. That would account for the failure.

  • Bstarr
    Avatar
    Community Member
    25 Posts

    Re: Pulling SS Blog from another pages Link to this post

    Bruce, that fixed it. Thanks so much!

  • scpi
    Avatar
    Community Member
    21 Posts

    Re: Pulling SS Blog from another pages Link to this post

    Along these lines, what would be the best way to extend this code to pull only blog posts with a specific tag? For example, to just pull posts tagged with "news" into a list on an "about us" page?

    I see a ShowTag() function in BlogHolder.php, but that's looking for URL parameters. SelectedTag() in BlogTree.php appears to do the same thing. Entries() in BlogTree.php looks promising, but I'm not sure I can get there from here. Everything else I'm coming up with is pretty convoluted. I feel like I'm missing something, and this ought to be easier than I'm making it.

  • scpi
    Avatar
    Community Member
    21 Posts

    Re: Pulling SS Blog from another pages Link to this post

    To follow up, I can make this work via SQL, borrowing some code from Entries() in BlogTree.php. But I feel dirty when I copy & paste code like this.

    function LatestBlogPosts($num=5, $tag='') {
    if($tag) {
    $SQL_tag = Convert::raw2sql($tag);
    $tagCheck = "AND \"BlogEntry\".\"Tags\" LIKE '%$SQL_tag%'";
    } else {
    $tagCheck = '';
    }
    $blogs = DataObject::get_one("BlogHolder");
    return ($blogs) ? DataObject::get("BlogEntry", "ParentID = $blogs->ID ".$tagCheck, "Date DESC", "", $num) : false;
    }

    3334 Views
Page: 1 2
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.