WordPress.org

Ready to get started?Download WordPress

Forums

How to show latest 5 posts at the end of single.php (8 posts)

  1. wpjongleur
    Member
    Posted 1 year ago #

    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
    Thanks
    Jongleur

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    try and code a secondary loop after the main loop in single.php;

    example:

    <?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//
    
    endwhile;
    endif;
    wp_reset_postdata();
    ?>

    http://codex.wordpress.org/Class_Reference/WP_Query

  3. wpjongleur
    Member
    Posted 1 year ago #

    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.
    thanks
    Jongleur

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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 http://codex.wordpress.org/Forum_Welcome#Posting_Code

  5. wpjongleur
    Member
    Posted 1 year ago #

    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?

  6. wpjongleur
    Member
    Posted 1 year ago #

    <?php get_header(); ?>
    </div><!-- end #left-->
    
    <div id="right">
    
    <img src="http://www.mysite.org/images/bigtop.gif" 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') ?>
    				</div>
    
    				<?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> 
    
    		<?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();
    
    			endwhile;
    			endif;
    			wp_reset_postdata();
    			?>
    
    	<?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="http://www.mysite.org/images/bigbot.gif" alt="big top">
    </div><!-- end #right -->
    
    <?php get_footer(); ?>
  7. wpjongleur
    Member
    Posted 1 year ago #

    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.

    next_post_link('<strong>%link</strong>');
    		?>	<div id="blog_details" class="entry-meta left-align nobreak">
    <?php comments_number( 'No Comments', '1 Comment', '% Comments'); ?><br>
  8. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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();
    
    			endwhile;

    to:

    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'); ?>
    </p>
    		<?php	endwhile;

    http://codex.wordpress.org/Function_Reference/the_title
    http://codex.wordpress.org/Function_Reference/the_permalink
    http://codex.wordpress.org/Function_Reference/comments_popup_link

    http://codex.wordpress.org/The_Loop
    http://codex.wordpress.org/Theme_Development

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.