Grow your CSS skills. Land your dream job.

WordPress Pagination with this Query

  • # August 31, 2010 at 7:31 am

    I’ve been using this query now for a while to get post titles and excerpts outside the loop. Does anyone know if there’s a way to amend the snippet I’m using to add pagination so that after so many post excepts are shown they’d break to another page if there are any more?

    	< ?php
    $myposts = new WP_Query();
    $myposts->query('cat=8&showposts=1');

    if ($myposts->post_count == 0) { ?>

  • < ?php } else {
    while ($myposts->have_posts()) : $myposts->the_post(); ?>

    Featured Match



    ">

    < ?php the_title(); ?>



    < ?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(200,300),array("class" => "alignleft post_thumbnail")); } ?>

    < ?php the_excerpt(); ?>


    < ?php endwhile;
    } ?>

    Thank you!

    # August 31, 2010 at 8:50 am

    Add the below to the end of your query:

    &paged=' . get_query_var('paged')); ?>
    # August 31, 2010 at 9:20 am

    Thanks for that, I tried adding it like this but it doesn’t work, have I inserted it in the wrong place/way?

      < ?php $recent = new WP_Query(); ?>

    < ?php $recent->query('cat=6&showposts=50&paged=' . get_query_var('paged')); ?>

    < ?php while($recent->have_posts()) : $recent->the_post(); ?>



    < ?php endwhile; ?>
    # August 31, 2010 at 4:52 pm

    You might need to substitute

    showposts=50

    With

    posts_per_page=10

    Of course, you will also need to add in your previous/next page links after your endwhile;

    # September 1, 2010 at 8:13 am

    I found a solution that seems to work nicely (I couldn’t get it to work with the other query). Here’s the code for anyone who wants to achieve the same thing.


      < ?php
      $temp = $wp_query;
      $wp_query= null;
      $wp_query = new WP_Query();
      $wp_query->query('cat=6&showposts=50'.'&paged='.$paged);
      ?>
      < ?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    • " rel="bookmark">< ?php the_title(); ?>

    • < ?php endwhile; ?>

    # September 5, 2010 at 12:26 pm

    @darfuria you’re thinking of query_posts.

    I’m not as familiar with WP_Query. Chris just did a screencast on the loop where he talked a little bit about query_posts.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".