WordPress.org

Ready to get started?Download WordPress

Forums

Having trouble with pagination. (3 posts)

  1. mmaumio
    Member
    Posted 10 months ago #

    I'm running WordPress 3.6.1 the latest version.

    Trying to paginate through WP_Query class. This's not working.
    I'm providing the code to have a look.

    I'm sure I did something silly that's out of my sight.

    [ Moderator note: please wrap code in backticks or use the code button. ]

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    
             <?php
    		 	$args = array ( 'posts_per_page' => 3, 'paged' => $paged );
    		 	$the_query = new WP_Query($args); // note that the $paged function doesn't seem to be working
    		 ?>
    
    		<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
    
            <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    
    			<h2><a>"><?php the_title(); ?></a></h2>
                  <p> <B>Date posted: <?php the_date(); ?></B> </p>
    			<?php include ('wp-includes/meta.php' ); ?>
    
    			<div class="entry">
    				<?php //the_content() ?>
                    <?php the_excerpt(); ?>
    
    			</div>
    
    		</div>
    
    	<?php endwhile; ?>
    	<div class="nav-previous alignleft"><?php next_posts_link(); ?></div>
    	<div class="nav-next alignright"><?php previous_posts_link(); ?></div>
        <?php wp_reset_postdata(); ?>
    
    	<?php else : ?>
    
    		<h2>Not Found</h2>
    
    	<?php endif; ?>

    Thanks.

  2. keesiemeijer
    moderator
    Posted 10 months ago #

    Add the $max_pages parameter to the next_post_link() function:
    https://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query

    div class="nav-previous alignleft"><?php next_posts_link( __( 'Next Page &raquo;' ), $the_query->max_num_pages ); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link(); ?></div>
  3. mmaumio
    Member
    Posted 10 months ago #

    Thanks a lot for the help.
    It almost worked.

    Except a single issue.
    When I hit the next page link it remains on the same page(blog page) raher than moving into the page2.

    Please suggest me something.

Reply

You must log in to post.

About this Topic