Support » Fixing WordPress » How to show latest 5 posts at the end of single.php

  • I want to be able to show the latest 5 posts in the same category -if none , show none- below the post on single.php? How can I do this?

    Also how can I format it (title of one size and color and no of comments in a different color and size)

    Please help

Viewing 7 replies - 1 through 7 (of 7 total)
  • try and code a secondary loop after the main loop in single.php;


    $post_cats = array();
    foreach( get_the_category() as $post_cat ) { $post_cats[] = $post_cat->term_id; }
    $related = new WP_Query( array( 'posts_per_page' => 5, 'category__in' => $post_cats ) );
    if( $related->have_posts() ) : while( $related->have_posts() ) : $related->the_post();
    //your output//

    I’m using DISQUS for comments. I tried adding your code, nothing is showing up now in the other posts area beneath the post in the single.php page.

    Please help.

    the suggested code is only the minimal WordPress code for your idea; no html structure and actual output elements so far;

    you need to add something to output the title, content/excerpt, postmetadate etc.

    what did you add into the code in place of ‘//your output//’ ?

    please post the full code of your single.php – see

    I didnt add anything there, thinking that that was a my typo. I didnt realise that was a placeholder.

    Please can you tell me how to output the title and the no of comments alone there?

    <?php get_header(); ?>
    </div><!-- end #left-->
    <div id="right">
    <img src="" alt="big top">
    <!-- begin #contactus-content-->
    <div id="admissions-content">
    <div id="generic-font">
    	<?php if (have_posts()) : ?>
    		<?php while (have_posts()) : the_post(); ?>
    				<h2><?php the_title(); ?></h2>
    				<div id="blog_details" class="entry-meta">
    				Filed in <?php the_category(', ') ?>
    				on <?php the_time('M') ?>  <?php the_time('j') ?>, <?php the_time('Y') ?>
    				<?php the_content('Read the rest of this entry ยป'); ?>
    				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    			<?php comments_template(); ?>
    		<?php endwhile; ?>
    		<b><u>Other Posts</u></b><br>
    			<span class="previous-entries"><?php previous_post_link(' %link') ?></span>
    			<span class="next-entries"><?php next_post_link('%link') ?></span> 
    			$post_cats = array();
    			foreach( get_the_category() as $post_cat ) {
    				$post_cats[] = $post_cat->term_id;
    			$related = new WP_Query( array( 'posts_per_page' => 5, 'category__in' => $post_cats ) );
    			if( $related->have_posts() ) : while( $related->have_posts() ) : $related->the_post();
    	<?php else : ?>
    		<h2>Not Found</h2>
    		<p>Sorry, but you are looking for something that isn't here.</p>
    	<?php endif; ?>
    </div></div><!-- end #admissions-content -->
    <img src="" alt="big top">
    </div><!-- end #right -->
    <?php get_footer(); ?>

    I tried adding the following code in the “your output” but the links are coming from different categories and the comments are not linked to the corresponding posts. Please help.

    		?>	<div id="blog_details" class="entry-meta left-align nobreak">
    <?php comments_number( 'No Comments', '1 Comment', '% Comments'); ?><br>

    to show the linked post title, and the comments number, try to edit this section:

    if( $related->have_posts() ) : while( $related->have_posts() ) : $related->the_post();


    if( $related->have_posts() ) : while( $related->have_posts() ) : $related->the_post(); ?>
    <p><a href="<?php the_permalink(); ?>" title="link to: <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    <br />
    <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?>
    		<?php	endwhile;

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to show latest 5 posts at the end of single.php’ is closed to new replies.