WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Yet Another Related Posts Plugin] split the results (6 posts)

  1. Tammy Hart
    Member
    Posted 4 years ago #

    I would like to split the results by capturing 4 of them, displaying the first one as usual, then putting the other three into a function that i can display somewhere else on single.php. It would roughtly be like this:

    > 1st related posts

    ---other stuff like tags and categories---

    > 3 more related posts
    >
    >

  2. @tammyhart, that shouldn't be too hard using the YARPP templates feature:

    http://mitcho.com/blog/projects/yarpp-3-templates/

    You can start with the yarpp-template-example code, then keep track of "firstness" by saying $i=0;, add a$i++; within the loop, then do the separate formatting through some conditional like if ( i == 0 ). Should be straightforward if you're comfortable with PHP. Hope that helps.

  3. Tammy Hart
    Member
    Posted 4 years ago #

    I will tinker with that some, but I'm pretty much a php noob :)

    Would this allow me to put the other three in a function that I can only use in the templates i want. For instance, on the homepage, i only want the first one, but on the single page, i want to have the other three as well.

    Thanks so much.

  4. @tammyhart yes, you could do that by wrapping the whole template code in a conditional like if (is_single()) and then just having two versions of the YARPP template code.

  5. Tammy Hart
    Member
    Posted 4 years ago #

    Thanks, I was able to get some help from my good pal, Aaron Forgue. For anyone else wanting to do this, here is the code he put in the template:

    <?php
    	$GLOBALS['relatedPosts'] = array();
    
    	if ($related_query->have_posts()) {
    		while ($related_query->have_posts()) {
    			$related_query->the_post();
    
    			$GLOBALS['relatedPosts'][] = array(
    				'permalink' => get_permalink(),
    				'title' => get_the_title()
    			);
    		}
    	}
    ?>
    
    <?php if (count($GLOBALS['relatedPosts'])) { ?>
    	<ol id="related">
    		<li><strong>Related Article:</strong> <a href="<?php echo $GLOBALS['relatedPosts'][0]['permalink']; ?>" rel="bookmark"><?php echo $GLOBALS['relatedPosts'][0]['title']; ?></a><!-- (<?php the_score(); ?>)--></li>
    	</ol>
    <?php } else { ?>
    	<p>No related posts.</p>
    <?php } ?>

    and here is the code in the single template:

    <?php if (count($GLOBALS['relatedPosts']) > 1) { ?>
    <h3 class="divider">More Related Posts</h3>
    	<ol id="more-related">
    	<?php for ($i = 1; $i < count($GLOBALS['relatedPosts']); $i++) { ?>
    		<li><a href="<?php echo $GLOBALS['relatedPosts'][$i]['permalink']; ?>" rel="bookmark"><?php echo $GLOBALS['relatedPosts'][$i]['title']; ?></a><!-- (<?php the_score(); ?>)--></li>
    	<?php } ?>
    	</ol>
    <?php } ?>
  6. Thanks for sharing Tammy.

Topic Closed

This topic has been closed to new replies.

About this Topic