WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the index.php file and editing this so that it displays two articles at a time. I'm using query_posts( $query_string . '&posts_per_page=2' ); before the main loop and it's displaying what I want correctly.

How can I have a page navigation at the bottom?

share|improve this question

migrated from stackoverflow.com Aug 25 '11 at 16:02

This question came from our site for professional and enthusiast programmers.

3  
do you have any reason not to use dashboard -> settings -> reading -> Blog pages show at most [] posts – Michael Aug 25 '11 at 16:18
    
    
are you asking for wordpress functions such as next_posts_link(), previous_posts_link(), or posts_nav_link() or pagination plugins? – Michael Aug 25 '11 at 18:16

As in the comments, it's not clear why you can't use the options: dashboard -> settings -> reading -> Blog pages show at most [] posts

However, if it is necessary that this be done via altering the query (say to target only specific pages, or archives) then this should really be done on pre_get_post:

For instance to change the post's per page for any archive for a term in the 'my-tax' taxonomy:

add_action('pre_get_posts','wpse26898_change_per_post');
function wpse26898_change_per_post( $query ){
    if( $query->is_main_query() && is_tax('my-tax') ){
        $query->set('posts_per_page'=>2);
    }
}
share|improve this answer

query_posts alters the main loop and messes up pagination.

Use new WP_Query to show your first 2 posts before the main loop.

<?php

$args = array(
    'posts_per_page' => 2,
    );

$myquery = new WP_Query( $args );

        while ( $myquery->have_posts() ) : $myquery->the_post(); 

//Do stuff

endwhile;

wp_reset_postdata();

//Run main loop
share|improve this answer

For custom queries you need to enable paging by including $paged in your query parameters.

$paged is a global that gets set/updated whenever you're within a query context. WordPress looks at the URL structure /page/x in the url to determine the page number, x being the number. It is then used by the standard paging functions eg. previous_posts_link() and next_posts_link() until it is overwritten or reset using wp_reset_query().

// new query that respects $paged
query_posts( $query_string . '&posts_per_page=2&paged=' . $paged );

if ( have_posts() ) :

    while ( have_posts() ) : the_post();

        get_template_part( 'post' );

    endwhile;

    // pagination links to show next 2 or previous 2 posts
    next_posts_link();
    previous_posts_link();

endif;

// return to the original URL query
wp_reset_query();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.