WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Proper pagination with WP_Query. How to? (10 posts)

  1. Sabomoth
    Member
    Posted 1 year ago #

    I am trying to get my pagination to work when using WP_Query on my static frontpage. But i am unable to.

    This is the code i have at the moment:

    <?php $the_query = new WP_Query( 'posts_per_page=3' );?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post();?>
    		<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
    		<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
    		<p><?php the_excerpt(); ?></p>
    <?php endwhile;?>
    
    <?php wp_reset_postdata();?>

    (Not showing the next/previously post link thingies.)

    Looking at the codex under WP_Query pagination i am supposed to add this:

    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    $query = new WP_Query( 'paged=' . $paged );

    I added the 'paged=' . $paged into the arguments of the function but it wont work. Am i doing something wrong? Something secret i have to do?

  2. Beee
    Member
    Posted 1 year ago #

    try the following

    <?
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args = array(
    		'posts_per_page' => 3
    		'paged' => $paged
    	);
    	$the_query = new WP_Query($args);
    
    	while ( $the_query->have_posts() ) : $the_query->the_post();
    		// your loop content
    	endwhile;
    ?>
    <div class="navigation">
    	<div class="nav-left"><?php previous_posts_link(); ?></div>
    	<div class="nav-right"><?php next_post_link(); ?></div>
    </div>
  3. Sabomoth
    Member
    Posted 1 year ago #

    Hi

    I gave that a try but it isnt working correctly. I am only getting 2 pages of results, yet i have 11 posts that are supposed to come up.

    I get the next page link, when i click on it i go to page 2, with 3 other posts, and there is a previous page link, the next page link does show on the second page.

  4. Beee
    Member
    Posted 1 year ago #

    do you have a link perhaps ?

  5. Sabomoth
    Member
    Posted 1 year ago #

    Its not online yet.
    And above is all the code i have that has to do with the loop and pagination. Its supposed to be on my front page(home.php).
    Maybe there is some secret stuff in functions i have to add or something?

  6. Beee
    Member
    Posted 1 year ago #

    have several queeries all like this working without a hickup

    if you click my name you see a website, you can send me a message there. You may also send it to me if you don't want it 'out there yet'.

    maybe if you add wp_reset_query(); before the args and this

    $temp = $wp_query;
    $wp_query = null;

    before

    $the_query = new WP_Query

    if this fails why not set the posts per page in Settings > Reading to 3 ?

  7. Rev. Voodoo
    Volunteer Moderator
    Posted 1 year ago #

    http://weblogtoolscollection.com/archives/2008/04/19/paging-and-custom-wordpress-loops/
    Here's an oldie but a goodie. Might just need to clean out your query prior to setting it up - since you are getting 2 pages on your pagination, it makes me wonder if it's hung up on the original / default query

    EDIT: the above response wasn't there when I sent mine ... similar suggestion

  8. Sabomoth
    Member
    Posted 1 year ago #

    I changed the settings the in settings>reading to 3 as well, and it worked. Seems like wp read is as showing 10 per page, and thus 11 only gets 2 pages. While it showed 3 per page.

    Should i add that reset thing to my query anyway just as a precausion for future problems?

    Thank you for you help. :)

  9. Rev. Voodoo
    Volunteer Moderator
    Posted 1 year ago #

    Setting the query to null prior to running your custom query should alleviate the problem...

    Your query on the posts is picking up the 3 per page setting, but the pagination was picking up the 10 per page overall setting.

    If you are happy with leaving your overall setting at 3 per page, all is well. If not, then the above suggestions would fix the issue.

  10. Beee
    Member
    Posted 1 year ago #

    the reset thing is not necessary I think, but you can test it and see what it does...

    you can always 'ignore' it by commenting it, just put // and a space before wp_reset_query();

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.