Jump to content

Assistance with some related post code


cmscritic
 Share

Recommended Posts

I don't claim to be a code expert, I'm the type who hacks until it looks like it's going to work. I simply am stuck on a specific point right now. I've created a variable in my post.php to call related posts, I'm using this code:

$related = count($page->related) . renderNav($page->related);

I'm then placing it where I want in my post later. What's happening at the moment is that it renders the following:

1 < - This i assume is the number of related posts.

related post title < - This is the post title

All is fine here except that I want to remove the number of related posts (or hide it) and show a title instead such as "Check out these related articles" in it's place.

Further to this, I'm not even sure the code I am using is correct. Ryan originally was using an if statement to generate my related posts in my sidebar. His code is as follows:
 

if(count($page->related)) $side .= "<h4>Related Stories</h4>" . renderNav($page->related);

However, in order to place it where i want below in the flow of my content, I need a variable since that's how the rest of the post.php is generated.

It currently looks like:

* Final post markup
 *
 */

$body = <<< _OUT

    <article class='post post-full' $attr>    
        <header>
            <h1>$headline</h1>
            $meta
            $rating
        </header>
        $bodycopy
        $tags
        $related < - This is what I added thinking I was being smart
        $addthis
        $footerad
        $authorBox
         $comments
    </article>

I added the $related and my code thinking this was the right way to do it but perhaps I'm wrong?

I'm simply not sure how to modify the code in order to do so. I'm sure this is easy but, like I said, I'm still new to doing this stuff.

Thanks in advance.

Link to comment
Share on other sites

//$related = count($page->related) . renderNav($page->related);
// instead of the above, try this:

// I assume from your old code, that this will keep the markup for related articles
$related = ''; 

// Check if there is more than 0 related pages
if ($page->related->count() > 0) {
  $related .= "<h2>Check out these related articles</h2>"; // Render headline
  $related .= "<ul>";
  foreach($page->related as $p) { // Loop through all related pages and then generate their markup
    $related .= "<li><a href='$p->url'>$p->title</a></li>";
  }
  $related .= "</ul>";
}

In your old code you are actually outputting the count and then something. What we don't know is your renderNav function (what it does), since it's not visible for us.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...