Forums

Individual post and post link list cannot place together in single.php (2 posts)

  1. Taiyou
    Member
    Posted 1 month ago #

    Hello,

    I'm now working on the single.php, try to make it having 2 parts. One for displaying all post's permalinks from category id "5", one for displaying the requested individual post, under the same category. With the code quote below, it only displays the link list, and the individual post not showing up. But if I move the "Category post list" part after the "Display post" part, it works. I tried to read the wordpress docs on templates and loops, but still not sure how to make it. Maybe I missed some important things? Thanks in advance for any kindness help.

    <?php
    get_header();
    ?>
    <!-- Category post list -->
    <?php query_posts('cat=5&showposts=-1'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div align="left">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
    </div>
    <?php endwhile; else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>
    <?php query_posts(); ?> <!-- I'm not sure is this necessary or not to clear the query -->
    <!-- Category post list ends-->
    
    <!-- Display post, original from default single.php -->
    <div id="content" class="archivecolumn" role="main">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    	<div class="fontstyle9"><?php the_time('Y- n- j') ?></div><br />
    		<div class="entry">
    			<?php the_content(); ?>
    		</div>
    	</div>
    	<?php endwhile; else: ?>
    		<p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>
    
    </div>
    <?php get_footer(); ?>
  2. Taiyou
    Member
    Posted 1 month ago #

    How come I tried for a whole week, and once posted here, I got an answer!
    (Actually I'm just inspired by how to display category post only)

    What I did is still moving the link list part on top. Then make the query a new name. I'm not sure the reason, but I guess it is now not mixing up with the if loop of the individual post.

    This is the final code I'm using. Just for any one facing the same problem. Thanks for reading ;p

    <?php
    get_header();
    ?>
    
    <div id="content" class="archivecolumn" role="main">
    
    <!-- Category post list -->
    <?php $postList = new WP_Query();
    $postList->query('cat=5&showposts=-1');
    ?>
    <?php while ($postList->have_posts()) : $postList->the_post(); ?>
    <div align="left">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
    </div>
    <?php endwhile; ?>
    <!-- Category post list ends-->
    
    <!-- Display post, original from default single.php -->
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    	<div class="fontstyle9"><?php the_time('Y- n- j') ?></div><br />
    
    	<div class="entry">
    		<?php the_content(); ?>
    	</div>
    	</div>
    	<?php endwhile; else: ?>
    		<p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>
    <!-- Display post ends-->
    
    	</div>
    <?php get_footer(); ?>

Reply

You must log in to post.

About this Topic