WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have just been following this tutorial - http://www.lateralcode.com/wordpress-pagination/

I have written that function in my functions.php file and taken numerous notes on how it works.

The problem now is I just don't know what to do with it to get it working. I want it running on my search results and my in a loop on my blog page.

Any ideas? I'm trying to learn this rather that use a plugin....

This is my code from my loop over ride file - loop-search.php

<h1>Results for: "<i><?php echo get_search_query(); ?></i>"</h1>
<?php if(have_posts()) { while(have_posts()) { the_post();?>

<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php }}?>


share|improve this question
You have added the pagination() function in your functions.php file? Then you only have to call it in the different templates where you want to use it. search.php, or archive.php, ... Which files are used can be seen in the Template Hierarchy. Can you edit your question and include the names of the files in your template directory? – Jan Fabry May 31 '11 at 13:34
Hi, I have an override for search results using loop-search.php - would it go in there or in search.php? Also, is it placed in the loop? thanks – Dan May 31 '11 at 13:44
I have put a section of my code on here now, this is once place i'd like to put the pagination function. – Dan May 31 '11 at 13:48
you could add the pagination function after the endwhile; of the loop in loop-search.php (which is missing in your posted code section, but should be there somewhere). – Michael May 31 '11 at 13:56
Hi Michael, the }} are my endwhile; and endif; I dropped the function in there, nothing happened. – Dan May 31 '11 at 14:00

If you added the pagination() function to your functions.php, you only have to call it in your theme files. Which file depends on where you want to display it, see the Template Hierarchy for details.

You don't want to call this inside The Loop (between the while () {} or while : ... endwhile;), because this will display it for each post again. Whether you then call it in your search.php or loop-search.php depends on your personal preference. Twenty Ten does it in the loop*.php files. Summarized, it would look like this:

<!-- Display pagination links above the posts -->
<?php pagination(); ?>
<!-- Display the posts -->
<?php if(have_posts()) { while(have_posts()) { the_post();?>
<!-- Some details for this post -->
<!-- End the loop -->
<?php }} /* if, while */ ?>
<!-- Display pagination links again below the posts -->
<?php pagination(); ?>

I recommend you to prefix the name of the pagination() function with something more unique. You never know when WordPress will include a function called pagination() itself. dan_pagination() is a possible alternative :-)

There are many pagination tutorials on the web, and many of them contain inaccuracies. So does the tutorial you linked to. Instead of using the total number of posts of the current query, it will count all posts in the blog to determine the number of pages. This will give errors on search pages. I would change the start of the function like this:

function wpse18805_pagination( $scope = 2 ) {
    global $wp_query;

    $numPages = $wp_query->max_num_pages;
    $curPage = $wp_query->get( 'paged' );
    if ( ! $curPage ) {
        $curPage = 1;

    // page bounds ... continue from there

This function will also generate links that always start counting from the base of the blog, so it won't work for search pages. To fix that, replace the line after // echo the link with:

echo '<li><a href="' . get_pagenum_link( $page ) . '">' . $page . '</a></li>';
share|improve this answer
I have changed the function name, and i'm calling it after the loop. It echo's absolutely nothing. The term I am searching for should be giving plenty of results and therefore need pagination. At the moment it gives me 10, which is a default number I imagine? Do I need to change something else? – Dan May 31 '11 at 14:08
Hi again Jan, I just used this function (kriesi.at/archives/…) and it worked - so there must be something wrong with that other one.. – Dan May 31 '11 at 14:25
@Dan: That function was indeed not very flexible. I have written some corrections, but if you were able to solve your problem I'm happy for you! – Jan Fabry May 31 '11 at 14:28
Can I see what you have written at all? I'm trying to learn as much as possible and would like to know what the issue was... – Dan May 31 '11 at 14:30

Your Answer


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.