WordPress.org

Support

Support » How-To and Troubleshooting » next_posts_link repeating same posts on each page

next_posts_link repeating same posts on each page

  • I am pretty new to php and wordpress but i have my site just about finished except the next_posts_link and previous_posts_link seem to be repeating the same posts on each page and i can’t seem to figure our how to fix it.

    The code from my index.php is below. Any advice is appreciated!

    <?php get_header(); ?>
    
    <div id="content">
    	<!--index.php-->
          <?php if (have_posts()) {query_posts('cat=13');} ?> 
    
      <!--the loop-->
        <?php while (have_posts()) : the_post(); ?>
    
          <!--post title as a link-->
          <div id="post-<?php the_ID(); ?>" class="small">
            <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                <!-- Get the Thumbnail -->
            <img src="<?php echo get_post_meta($post->ID, "Thumbnail", true); ?>" alt="<?php the_title(); ?> <?php _e(''); ?>" height="150" /></a>
                <!-- set the read more link -->
     <?php excerpt(75); ?>
    
         </div>
    	        <!--end of one post-->
    	<?php endwhile; ?>
    </div>
    		<!--navigation-->
    
    <div id="Navigation">
      <style type="text/css">
        h6 {text-align:center; font-size:75%;
        font-family:Arial,Verdana,Sans-serif;
        color:#000}
        p.date {text-align:right}
        p.main {text-align:justify}
      </style>
    </div>
    
    	<!--do not delete-->
    	<?php else : ?>
    
    		Not Found
    		Sorry, but you are looking for something that isn't here.
    		<?php include (TEMPLATEPATH . "/searchform.php"); ?>
            <!--do not delete-->
    
    	<?php endif; ?>
    
    <!--index.php end-->
    
    <div id="navigation">
     <?php previous_posts_link('&laquo; Previous') ?>
     <?php next_posts_link('More &raquo; ') ?> 
    
    </div>
    
    <!--include footer-->
    <?php get_footer(); ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • I suspect the following line is what’s doing it:

    <?php if (have_posts()) {query_posts('cat=13');} ?>

    What that line does is to reset the query every time the page is loaded, so you’re always loading the first XX posts from the category with an ID of 13 (where XX is the number of posts you’ve specified in your Settings -> Reading options)

    Thanks Curtiss!
    i do want to only display the posts with a category of 13 – what’s the best way to go about fixing this?

    it looks like you’re right – if i replace that line with
    <?php if (have_posts()) : ?>

    the pages go through the posts as expected – now the problem is they aren’t limited to category id 13

    You’ll need to modify the $wp_query->query_vars array prior to the query being executed (so you need to add some code to your theme’s functions.php file).

    You can see an example of excluding a category in this codex article. It’s the same principle, except you won’t use the '-' . in front of your category ID.

    or try and replace:

    <?php if (have_posts()) {query_posts('cat=13');} ?>

    replace with:

    <?php global $query_string; query_posts($query_string . '&cat=13'); if (have_posts()) : ?>

    similar to http://codex.wordpress.org/Function_Reference/query_posts#Usage_Note

    (untested)

    Alchymyth that worked great!! thank you both so much!!

    wait – i take that back – still not limiting to category id 13 it seems

    what is showing?

    be aware that cat=13 will also get posts in the child categories of cat13

    possilbly, try:

    global $wp_query;
    $args = array_merge( $wp_query->query, array( 'category__in' => array(13) ) );
    query_posts( $args );

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘next_posts_link repeating same posts on each page’ is closed to new replies.
Skip to toolbar