• Resolved michaelhyatt

    (@michaelhyatt)


    I am a semi-novice and a hack, so be gentle. 😉

    I want to add pagination to this page like I have on my home page (scroll to bottom).

    The code for this page looks like this:

    <?php
    /*
    Template Name: Resources
    */
    ?>
    
    <?php get_header(); ?>
    
      <?php $my_query = new WP_Query('category_name=Resources&showposts=50');
      global $my_query;
      $my_query->in_the_loop = true; 
    
      global $wp_query;
      $wp_query->in_the_loop = true;
    
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>
    
    	<!--Start Post-->
    	<div class="post">
    
    		<div class="p-head">
     			<p class="p-date"><?php the_time('l, F j, Y') ?></p>
    			<h1><?php the_title(); ?></h1>
    		</div>
    
    		<div class="p-con">
    			<?php the_content(); ?>
    		</div>
    
    		<?php the_tags('Resource: &nbsp',', '); ?>
    
    	</div>
    
    <?php endwhile; ?>
    
    <?php get_footer(); ?>

    I have searched the forum and, for the life of me, can’t figure this out. Can someone please help me?

    Thanks,

    Mike

Viewing 15 replies - 1 through 15 (of 15 total)
  • Check out these two in the codex…

    http://codex.wordpress.org/Template_Tags/previous_posts_link

    http://codex.wordpress.org/Template_Tags/next_posts_link

    These two tags would go after the endwhile statement towards the end of the code you posted. If you want to style them the same as other pages, I suggest you look at a page with the pagination on it, and see how the html and css is set up.

    Looking at one of your pages, I found this…

    <!--Start Post-->
    <div class="nav">
     <a href="http://michaelhyatt.com/page/3" ><div class="left">Older Entries</div></a> <a href="http://michaelhyatt.com/" ><div class="right">Newer Entries</div></a></div>
    <script src = 'http://intensedebate.com/js/wordpressTemplateLinkWrapper2.php?acct=b3fc732c0b82c31b272f01cfa79c97e5' type='text/javascript'></script></div>
    <!-- End SC -->

    Hope this helps. I’m relatively new to WordPress, so if I’m wrong on any of the above, someone let me know! 🙂

    Thinking about it, it might be easier if you looked at a page on which the pagination is working, offline. If you don’t have the theme on your hard drive, download it via FTP, then hunt for those template tags in one of your theme’s pages. I’m assuming it’ll be in the index.php file. Then copy and paste the relevant bit of html and code into your new page, and upload it to your theme’s folder.

    Suggest you change:

    <?php $my_query = new WP_Query('category_name=Resources&showposts=50');

    to

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $my_query = new WP_Query('category_name=Resources&showposts=50&paged=' . $paged');

    At if you want links to previous and next, look at the WordPress Default theme’s wp-content/themes/default/index.php for usage of the next_posts_link and previous_posts_link templates.

    Thread Starter michaelhyatt

    (@michaelhyatt)

    This is a big help. However, the pagination is still not showing up. I want 10 entries to show up per page. Here’s what I have now. Sorry to be so dense.

    <?php
    /*
    Template Name: Resources
    */
    ?>
    
    <?php get_header(); ?>
    
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $my_query = new WP_Query('category_name=Resources&showposts=5&paged=' . '$paged');
    
    global $my_query;
      $my_query->in_the_loop = true; 
    
      global $wp_query;
      $wp_query->in_the_loop = true;
    
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>
    
    	<!--Start Post-->
    	<div class="post">
    
    		<div class="p-head">
     			<p class="p-date"><?php the_time('l, F j, Y') ?></p>
    			<h1><?php the_title(); ?></h1>
    		</div>
    
    		<div class="p-con">
    			<?php the_content(); ?>
    		</div>
    
    		<?php the_tags('Resource: &nbsp',', '); ?>
    
    	</div>
    
    <?php endwhile; ?>
    
    <!--Start Post-->
    <?php include("nav.php"); ?>
    
    <?php get_footer(); ?>

    Here’s the output this code produces.

    showposts=10 will get you 10 posts per page.

    not sure what nav.php does, but you will also want to review what was said above about next and previous posts links.

    Thread Starter michaelhyatt

    (@michaelhyatt)

    This still doesn’t show the navigation controls. The code now looks like this:

    <?php
    /*
    Template Name: Resources
    */
    ?>
    
    <?php get_header(); ?>
    
    <?php
    	$show_posts = '10';
    	$cat_name = 'Resources';
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    	$my_query = new WP_query('category_name=' . $cat_name . '&showposts=' . $show_posts . '&paged=' . $paged);
    
    	global $wp_query;
      	$wp_query->in_the_loop = true;
    
      	while ($my_query->have_posts()) : $my_query->the_post();
      	$do_not_duplicate = $post->ID;?>
    
    	<!--Start Post-->
    	<div class="post">
    
    		<div class="p-head">
     			<p class="p-date"><?php the_time('l, F j, Y') ?></p>
    			<h1><?php the_title(); ?></h1>
    		</div>
    
    		<div class="p-con">
    			<?php the_content(); ?>
    		</div>
    
    		<?php the_tags('Resource: &nbsp',', '); ?>
    
    	</div>
    
    <?php endwhile; ?>
    
    <!-- Insert Navigation -->
     <<?php include("nav.php"); ?>
    
    <?php get_footer(); ?>

    <?php posts_nav_link(' — ', __('&laquo; Newer Posts'), __('Older Posts &raquo;')); ?>
    Place this outside your loop and it should work. http://www.twitter.com/bfegter

    Thread Starter michaelhyatt

    (@michaelhyatt)

    I already had the navigation calls outside the loop.

    As it turns out, I have taken another approach and solved the problem.

    Thanks.

    may i ask what your approach was.. cause I’m having the same problem… 🙂

    I’ve tried this solution before and it doesn’t work for me either. When I tried it on WP 2.7 the pages showed up but navigating them doesn’t work. See this site: http://www.fancountry.com

    Now I’m using pretty much the same code in WP 2.8.2 and not getting any pages showing up.

    MichaelH Moderator, any idea why? Here’s my code (note: this custom loop comes after a standard loop on the same page. not sure if this effects the outcome?):

    <?php
    $my_query = new WP_Query();
    $catname = wp_title('', false);
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $my_query->query('category_name=$catname&paged=$paged&posts_per_page=5');
    ?>
    
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php the_title(); ?>
    <?php } ?>
    
    <?php endwhile; ?>
    
    <?php next_posts_link('&laquo; Older Entries') ?>
    <?php previous_posts_link('Newer Entries &raquo;') ?>

    Got it to work!!! Finally!!! Using WP 2.8.2

    Problem with my code was that I was pulling the page name (to use to pull posts from the category of the same name) after defining the new WP_Query.

    One other strange thing was that I dropped the line for $paged definition and it still works. Here’s the code:

    <?php
    $catname = wp_title('', false);
    $wp_query = new WP_Query();
    $wp_query->query('category_name='.$catname.'&showposts=5'.'&paged='.$paged);
    ?>
    
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <?php the_title(); ?>
    <?php } ?>
    
    <?php endwhile; ?>
    
    <?php next_posts_link('&laquo; Older Entries') ?>
    <?php previous_posts_link('Newer Entries &raquo;') ?>

    Hope this helps

    The reason why funkboybilly’s solution works is because he is overwriting the global $wp_query variable, so the next_post_link() function knows the pages.

    A slightly modified solution would be:

    <?php
    $catname = wp_title('', false);
    $new_query = new WP_Query();
    $new_query->query('category_name='.$catname.'&showposts=5'.'&paged='.$paged);
    ?>
    
    <?php while ($new_query->have_posts()) : $new_query->the_post(); ?>
    <?php the_title(); ?>
    <?php } ?>
    
    <?php endwhile; ?>
    
    <?php next_posts_link('&laquo; Older Entries', $new_query->max_num_pages) ?>
    <?php previous_posts_link('Newer Entries &raquo;') ?>

    This will preserve the $wp_query… basically just need to add the $new_query->max_num_pages argument to the next_posts_link function.

    Enjoy.
    Lew

    Hi,

    Using the great code from layotte, I’ve managed to add page links in the following way:

    [ 1 ] [ 2 ] [ 3 ] [ Next ]

    here’s the code:

    <?php
    //The Query
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $new_query = new WP_Query();
    $new_query->query( 'showposts=10&cat='.$category_id.'&paged='.$paged );
    
    //The Loop
    while ($new_query->have_posts()) : $new_query->the_post();
    
    // display posts as you need
    
    endwhile;
    
    // pager
    if($new_query->max_num_pages>1){?>
        <p class="pager">
        <?php
        for($i=1;$i<=$new_query->max_num_pages;$i++){?>
            <a href="<?php echo get_category_link($category_id).'page/'.$i;?>" <?php echo ($paged==$i)? 'class="active"':'';?>><?php echo $i;?></a>
            <?php
        }
        if($paged!=$new_query->max_num_pages){?>
            <a href="<?php echo get_category_link($category_id).'page/'.$i;?>">Siguiente</a>
        <?php } ?>
        </p>
    <?php } ?>

    Hope you find it useful!

    Hi, thanks to you I finally found the solution for my pagination problem! Really useful! I used it in a slightly different way. Still there are a few problems:

    1) Does anybody of you know if there’s a possibility to put this script in front of ‘the loop’ instead of after? I’d like to have pagination above my posts as well.

    2) And I miss a ‘previous’ link when on page 2 or further, any ideas? My ‘next’ link needs some work too, can somebody tell me what it is I’m doing wrong?

    This is the code I have now:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $loopb = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => 2, 'paged' => $paged ) ); ?>
    <?php while ( $loopb->have_posts() ) : $loopb->the_post(); 
    
    //display posts
    ?>
    <?php endwhile; ?>
    <?php
    if($loopb->max_num_pages>1){?>
        <p class="navrechts">
        <?php
        for($i=1;$i<=$loopb->max_num_pages;$i++){?>
            <a href="<?php echo '?paged=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
            <?php
        }
        if($paged!=$loopb->max_num_pages){?>
            <a href="<?php echo '?paged=' . $i; //next link ?>">></a>
        <?php } ?>
        </p>
    <?php } ?>

    Hope someone can help me!

    With slight changes, your code will work anywhere after your query, i.e. before or after the loop:

    <?php
    if($loopb->max_num_pages>1){?>
        <p class="navrechts">
        <?php
          if ($paged > 1) { ?>
            <a href="<?php echo '?paged=' . ($paged -1); //prev link ?>"><</a>
                            <?php }
        for($i=1;$i<=$loopb->max_num_pages;$i++){?>
            <a href="<?php echo '?paged=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
            <?php
        }
        if($paged < $loopb->max_num_pages){?>
            <a href="<?php echo '?paged=' . ($paged + 1); //next link ?>">></a>
        <?php } ?>
        </p>
    <?php } ?>

    EDIT: I did have to add ‘&page_id=206’ to the href because I am testing with a special page template, but I removed it before posting here. Without the addition, the links took me back to my home page.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Adding Pagination to a WP_Query Loop’ is closed to new replies.