Build an Advanced Custom Archive Page for WordPress Blog

Previously, I wrote an article describing about how to build archive page for WordPress blog without plugin. I was also using exactly that same procedure in this blog for the complete archive page. It works properly, but the problem with that procedure is that it shows all the date based archive posts from the blog in a single page.

Later, I tried to insert the pagination property in the coding section to split the page in sub pages. But could not get any success. Even I did not find any solution for that problem when searching in Google. Atlast, after a year, I’ve succeeded to insert the pagination in the custom archive page template. The procedure is simple and very similar to the previous codes.

All that you have to do is to add some new code with that. Lets proceed..

Follow the previous procedure and find out the following line.

<?php
// Declare some helper vars

Now add the following code before that.

<! --start code-->
<?php $temp = $wp_query;
 $wp_query= null;
 $wp_query = new WP_Query();
 $wp_query->query('showposts=60'.'&paged='.$paged); ?>
<! -- end code -->

Note: You can change the number of posts to be shown in a page. Here it is 60.

Again look for the line below:

$myposts = get_posts('numberposts=-1&orderby=post_date
&order=DESC');

Replace that by

$myposts = get_posts('orderby=post_date&order=DESC&showposts=60'.'&paged='.$paged);

Again, find out the line where the loop ends i.e look for the line

<?php endforeach; ?>
</ul>

Add the following code after that line.


<! --start code-->
<div class="pagenavi">
<?php if(function_exists('wp_pagenavi')) : ?>
<?php wp_pagenavi() ?>
<?php else : ?>
<span class="newer"><?php previous_posts_link(__('&lt;&lt; Newer Entries', 'inove')); ?></span>
<span class="older"><?php next_posts_link(__('Older Entries  &gt;&gt;', 'inove')); ?></span>
<?php endif; ?>
</div>
<?php $wp_query = null; $wp_query = $temp;?>
<! -- end code -->

Save the archive template. You should wisely insert appropriate place holder and CSS attributes for the design purpose. You can download the complete code from here.

let me know if you like use the plugin or the above custom template code for your blog archive.

5 Readers posted their valuable comments on the article - Build an Advanced Custom Archive Page for WordPress Blog
  1. approximate

    Hi Tanmay, I used this code but can you show how to get page numbers like yours instead of older and newer entries?

  2. approximate

    I didn’t want to install a plugin that’s why looked into this solution. Anyways thanks for the help. Appreciate it! :)

  3. Tanmay

    Approximate: The inbuilt pagination of WordPress includes the Older and Newer options. But if you want to to insert numbered pagination, you should use the plugin otherwise it will take lot of coding and css designing. I just wanted to minimize the use of plugins in my site. And I have using the pagination plugin for all the pages of my site. This archive page coding just avoided adding another plugin. Though after reading your feedback, I’ll try to find out some simple pagination coding without plugin. Stay tuned. Thanks for your comment.

  4. Flore

    Hi Tanmay,
    Thank you so much for this code, you are my personal hero at this moment: it’s been 2 days I was trying to fix the pagination of my website!

Leave a Reply

Your email address will not be published. Required fields are marked *