WordPresses pagination function relys on the variable $wp_query. Almost every theme you can find for WordPress has paging built in, most likly relying on two functions within the template -
and
. These functions are tied to the variable $wp_query, which is an instance of the WP_Query.
This is where we run into trouble with our custom loop.
Within our loop, we are creating our own instance of WP_Query which dosen't have the variable $wp_query. This is why our pagination will not work in our, or any custom loop.
So in order for us to have the pagination system work within our custom loop, we need to make WordPress beleive it's using the $wp_query variable in our custom loop. Basicly what we need to do is grab and temporarly store the variable reference to $wp_query. We set the variable of $wp_query to 'null' and then tell WordPress to begin our instance of WP_Query. This is where the magic happens, allowing us to set our own parimeters of $wp_query for our custom loop.
Lets put it together:
$customtemp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged);
The last line in our code above is the money-maker for us. The query 'paged=' & .$paged are the keys to making our custom loop pagination work. Without them we wouldn't be able to link the corosponding pages together.
So open up the loop we made yesterday and add it in at the very begining like this (I've highlighted the new parts):
<?php
<!– START custom loop –>
$customloop = 0; $customloop2 = 0;
$customtemp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged);if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();
$customloop++; $customloop2++;
if( $post->ID == $do_not [.....]
The last highlighted line that we added initiates the custom loop using $wp_query. Since This variable is used to initiate the loop at the beging, it will make sure the pagination works.
Now all we do is reset the $wp_query:
[.....] </div>
<?php wp_link_pages(); ?>
<!– END custom loop –>
<?php endwhile; endif; ?>
<?php $wp_query = null; $wp_query = $customtemp;?>
<div class="custom-pagination">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
That's it! Now our pagination will work in our custom loop. Not too hard eh?
We are almost done with our loop. At this point we have the main part of our custom loop set-up. Our style will come from the themes, so not too much is needed YET. I didn't add any major defining remarks because every persons use will be different, making the CSS style change also. I left the font color, size, background, h2 tags and a few other CSS features out that would be adopted from your main theme (in this case Kubrick). This is just the loop remember. It's up to you to fully design how it's going to look, but I'm sure you get the general idea of where to go with it.
Tomorrow we are going to move onto icorporating the loop into the template and make our pagination killer! Hope you come back and have a look...
the tags: development, functions, loop, page, pagination, php, theme, tutorial, wordpress










August 18th, 2010 on 4:43 am
Great post! Thanks for writing it. Can’t wait for Part 3!