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 query_posts to build a list of posts from a single category, and I'm trying to add pagination to this setup. Here's my code:

<ul>
    <h1><?php the_title(); ?></h1>
        <?php
    $paged = get_query_var( 'paged' );
    $args = array( 'posts_per_page' => 5, 'category' => 6, 'order' => 'DESC', 'paged' => 1 );
    $myposts = query_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
        <li>
            <strong><?php the_title(); ?></strong>
            <?php the_content(); ?>
        </li>
    <?php endforeach; 
    wp_reset_postdata();?>
</ul>
<?php echo next_posts_link()?>

<?php echo next_posts_link()?> generates a "Next Page >>" link, which links to the current URL with &paged=2 appended, but does nothing when clicked (no page loads occur whatsoever).

How can I get working pagination?

share|improve this question
2  
1  
Additionally read Rarst's answer to When should you use WP_Query vs query_posts() vs get_posts()? and Stephen Harris' answer to When to use WP_query(), query_posts() and pre_get_posts, because there almost never is a reason to use query_posts in the first place. –  ialocin Mar 24 at 21:23

2 Answers 2

You should use the following expression to compute paged parameter for the query.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

Instead of using query_posts i recommend you to use pre_get_pots action hook

Visit this page for more information.

share|improve this answer
    
For some reason, that doesn't work, and I've tried both examples on the page you linked to (neither of which will take the category parameter, for some reason, and end up displaying nothing). –  at least three characters Aug 18 '13 at 19:35
    
You have used query_posts along with foreach loop and setup_postdata() function instead you should use while loop the_post() function formmore information visit this page codex.wordpress.org/Function_Reference/query_posts –  Vinod Dalvi Aug 18 '13 at 19:47

Try this:

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

$args = array(
    'posts_per_page' => 5,
    'category'       => 6,
    'order'          => 'DESC',
    'paged'          => $paged,
);

Currently, you are setting The paged argument to 1.

share|improve this answer
    
Nope, still nothing. Could it have something to do with how I'm getting the link to generate (using <?php echo get_next_posts_link()?>)? EDIT: Using your solution, when I manually append &paged=2 to the URL, it takes me to the second page, but the link (which is properly wrapped in an <a> tag), just doesn't work. –  at least three characters Aug 19 '13 at 3:29
    
To clarify: hovering above the link makes my browser display the correct URL, with &paged=2 included, in the little destination tooltip thing in the bottom left corner. Inspecting the code shows correct markup for a link leading to that page. Hovering above the link changes the cursor, but actually clicking the link has it acting as if I were clicking un-linked text. –  at least three characters Aug 19 '13 at 3:38
    
Can you show the anchor <a> tag from the page source? –  Charles Clarkson Aug 19 '13 at 3:45
    
I prefer not to post the domain name, but the page is http://mydomain.com/?page_id=600 and the a href attribute is http://mydomain.com/?page_id=600&paged=2 –  at least three characters Aug 19 '13 at 3:58
    
I suspect that a JavaScript script is changing the destination of the link. Is href the only attribute in the <a> tag? –  Charles Clarkson Aug 19 '13 at 4:03

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.