-1

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?

1

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.

  • 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. – Jules 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. – Jules 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 – Jules 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
0

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.

  • 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). – Jules 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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