Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
    
helpful link: skyje.com/2010/02/5-excellent-wordpress-pagination-solutions –  Tara Aug 25 '11 at 16:21
    
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
add comment

3 Answers 3

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
add comment

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
add comment

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
add comment

Your Answer

 
discard

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