http://themethesis.com/wp-content/uploads/2010/06/wordpress-jquery-archives.png

Create a Powerful jQuery-Powered Archives Page in WordPress

June 1, 2010

in Tutorials

Wordpress jQuery Archives

Almost every blog you come across will have an archives page. Unfortunately, most of them will only show you a list of posts by month. An archives page is a prime place to get a reader further into your site.

Here you’ll learn how to add an archives page to WordPress that not only shows a monthly archive, but also your categories, every post you’ve written, your most popular posts, and even a search box, using jQuery to keep everything organized and easy to navigate.


This is based on the archives tutorials by WP Guy and Matt Flies, so thanks to them for the great work they’re doing. You can see a demo of this in action here on Theme Thesis.

Note: I will be teaching you how to create this page on both a regular WordPress site, and a Thesis site. Instructions for Thesis will be in a box like this.

  1. Create the archives page

    If you’re using a regular WordPress theme, you’ll need to create a new file called custom_archives.php and place the following at the top:

    <?php
    /*
    Template Name: Custom Archives
    */
    ?>
    

    If you’re using Thesis, you’ll need to create a custom function since there’s already an archives page by default.

    Place the following in your custom-functions.php file:

    function my_archive() {
    ?>
    
    //// CODE HERE
    
    <?php }
    remove_action('thesis_hook_archives_template', 'thesis_archives_template');
    add_action('thesis_hook_archives_template', 'my_archive');
    

    You will place the code in the following step in the middle.

  2. Add the skeleton

    Place the following code in the appropriate places as defined in the last step.

    
    <div class="archive-leftcol">
    
    <div class="archive">
      <h3>By Category</h3>
    
      <ul>
       	 	//  CATEGORY CODE
      </ul>
    
    <h3>All Articles</h3>
    
       <ul>
         		// ARTICLE CODE
       </ul>
    
    // MONTHLY ARCHIVES CODE
    
    <h3>Search</h3>
    
    <ul>
    		// SEARCH CODE
    </ul>
    </div>
    
    </div>
    
    <div class="archive-rightcol"> 
    
    <div class="archive">
    <h3>Popular Posts</h3>
    <ul>
    		// POPULAR POSTS CODE
    </ul>
    </div>
    
    </div>
    

    That’s the basic framework for the page. We set up two columns, one of which contain the Categories, All Articles, Monthly Archives, and Search, which will all be collapsed by default. The other column will show our popular posts and be open the entire time. Feel free to adjust and move anything around if you’d like. The most important thing to note is that each column is wrapped in a div named “archive”, uses h3 tags for the headings, and places its content in ul tags. This is what the jQuery will be working with shortly.

  3. Add the CSS

    The CSS is incredibly simple here. All you really need is:

    .archive h3 {cursor: pointer;}
    .archive-leftcol { float: left; width: 48%;}
    .archive-rightcol { float: right; width: 48%;}
    

    That’s it for the layout. That sets up two columns and sets the heading cursor to a pointer, so that your readers will know they can be clicked. You can also style .archive li{} if you’d like to make things prettier.

  4. Add the jQuery

    If you’re using a regular WordPress theme, open up header.php and place the following in the < head > section above < ?php wp_head(); ? >.

    
    <?php wp_enqueue_script("jquery"); ?>
    
    <script type="text/javascript">
    $(document).ready(function() {
      $('div.archive:eq(0)> ul').hide();
      $('div.archive:eq(0)> h3').click(function() {
        $(this).next().slideToggle('fast');
      });
    });
    
    </script>
    

    If you’re using Thesis, turn on jQuery in your Page Options and place the following function in custom-functions.php:

    function show_hide() {
    ?>
    
    <script type="text/javascript">
    $(document).ready(function() {
      $('div.archive:eq(0)> ul').hide();
      $('div.archive:eq(0)> h3').click(function() {
        $(this).next().slideToggle('fast');
      });
    });
    
    </script>
    
    <?php }
    add_action('wp_head', 'show_hide');
    

    So what does this do? It sets up jQuery to hide anything wrapped in ul tags inside of archive divs, and show them when you click on their headings.

  5. Add functionality

    Place the following code in the appropriate spot as defined by the comments in the skeleton code. That is, replacing the lines that start with “//”

    Category list

       <?php wp_list_categories('sort_column=name&amp;title_li='); ?>
     [sourcecode]  
    
    <h3>All articles</h3>
    
    [sourcecode language='php']
     <?php wp_get_archives('type=postbypost'); ?>
    

    Monthly archives

    This code was taken from WP Guys tutorial and slightly modified.

    <?php
    
    // Declare some helper vars
    $previous_year = $year = 0;
    $previous_month = $month = 0;
    $ul_open = false;
    
    // Get the posts
    $myposts = get_posts('numberposts=-1&amp;orderby=post_date&amp;order=DESC');
    
    ?>
    
    <?php foreach($myposts as $post) : ?>	
    
    	<?php
     	global $post;
    	// Setup the post variables
    	setup_postdata($post);
    
    	$year = mysql2date('Y', $post->post_date);
    	$month = mysql2date('n', $post->post_date);
    	$day = mysql2date('j', $post->post_date);
    
    	?>
    
    	<?php if($year != $previous_year || $month != $previous_month) : ?>
    
    		<?php if($ul_open == true) : ?>
    		</ul>
    		<?php endif; ?>
    
    		<h3><?php the_time('F Y'); ?></h3>
    
    		<ul>
    
    		<?php $ul_open = true; ?>
    
    	<?php endif; ?>
    
    	<?php $previous_year = $year; $previous_month = $month; ?>
    
    	<li><span class="the_article"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span></li>
    
    <?php endforeach; ?>
    	</ul>
    

    Search

    <form method="get" class="search_form" action="<?php bloginfo('home'); ?>/">
    		<p>
    			<input class="text_input" type="text" value="Enter search..." name="s" id="s" />
    			<input type="submit" id="searchsubmit" value="Search" />
    		</p>
    	</form>
    

    Popular posts

    To automatically show your most popular posts, you’ll need to install Rob Marsh’s Popular Posts plugin and then use the following:

    &lt;?php popular_posts(); ?&gt;
    

    Alternatively, you can manually put posts you’d like to feature by using a list.

  6. All together now

    This is what all the code should look like when you’re finished:

    <div class="archive-leftcol">
    
    <div class="archive">
      <h3>By Category</h3>
      <ul>
        <?php wp_list_categories('sort_column=name&title_li='); ?>
      </ul>
    
       <h3>All Articles</h3>
       <ul>
         <?php wp_get_archives('type=postbypost'); ?>
       </ul>
    
    <?php
    
    // Declare some helper vars
    $previous_year = $year = 0;
    $previous_month = $month = 0;
    $ul_open = false;
    
    // Get the posts
    $myposts = get_posts('numberposts=-1&amp;orderby=post_date&amp;order=DESC');
    
    ?>
    
    <?php foreach($myposts as $post) : ?>	
    
    	<?php
     	global $post;
    	// Setup the post variables
    	setup_postdata($post);
    
    	$year = mysql2date('Y', $post->post_date);
    	$month = mysql2date('n', $post->post_date);
    	$day = mysql2date('j', $post->post_date);
    
    	?>
    
    	<?php if($year != $previous_year || $month != $previous_month) : ?>
    
    		<?php if($ul_open == true) : ?>
    		</ul>
    		<?php endif; ?>
    
    		<h3><?php the_time('F Y'); ?></h3>
    
    		<ul>
    
    		<?php $ul_open = true; ?>
    
    	<?php endif; ?>
    
    	<?php $previous_year = $year; $previous_month = $month; ?>
    
    	<li><span class="the_article"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span></li>
    
    <?php endforeach; ?>
    	</ul>
    
    <h3>Search</h3>
    <ul>
    	<form method="get" class="search_form" action="<?php bloginfo('home'); ?>/">
    		<p>
    			<input class="text_input" type="text" value="Enter search..." name="s" id="s" />
    			<input type="submit" id="searchsubmit" value="Search" />
    		</p>
    	</form>
    </ul>
    </div>
    </div>
    
    <div class="archive-rightcol">
    <div class="archive">
    <h3>Popular Posts</h3>
    <ul>
    &lt;?php popular_posts(); ?&gt;
    </ul>
    </div>
    
    </div>
    
  7. Enjoy!

    So that’s the end. Now you should have a rockin’ archives page that should help your visitors find what they’re looking for and hang around your site a little longer.

References, resources, and inspiration:

An Archives Page With all the Posts in Chronological Order by WPGuy
10 Ways to Customize Thesis and Enhance Your Blog by Matt Flies
More Showing, More Hiding by Learning jQuery

Have a question, know a better way of doing something, or just want to say hey? Leave a comment below.

photo by the wolf

Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

You'll notice this site talks about the Thesis theme for Wordpress. Don't know what the heck that is? Find out.

{ 21 comments… read them below or add one }

Matt Langford June 1, 2010 at 9:03 pm

Great tutorial, Matt. Glad mine was able to, at the very least, inspire this one! Excellent job.

Reply

bannersky June 2, 2010 at 10:56 am

Great!
Thanks for sharing.

Reply

ivan June 2, 2010 at 4:50 pm

great tutorial… i will try this when i have some time thanks!

Reply

veer June 4, 2010 at 6:53 am

Hi,
i’m new to wordpress and i want to add jquery slideshow into my footer but it is not working if i use it in header it is working fine what is the problem give me some solutions.
thanks in advance.

Reply

Eko Setiawan June 4, 2010 at 3:37 pm

I’ll try this on my site…thank for this tutorial..Great!

Reply

Flinn June 4, 2010 at 8:54 pm

thnx a lot,m gonna do this,once m done with the 404 page.thnx a lot.

Reply

David Alexander July 20, 2010 at 6:00 am

Is the same possible with the new custom post types? and is it just a case of using an if statement? Great article by the way.

Reply

El Jayesse August 29, 2010 at 8:26 pm

thank you very much! you have a great clarity in explaining and teaching!
i have a question about section 6. line 42: there is a closing … but no opening to it. is it missing?

Reply

Matt Dunn September 10, 2010 at 3:46 pm

No, it opens in an if statement.

Reply

john Mackenzie September 11, 2010 at 7:53 pm

thanks for this mod it looks promising but I cant get it working. I get this error when i add the jQuery code to the header:

Message: Object doesn’t support this property or method
Line: 14
Char: 1
Code: 0

now I am not using Thesis, am using some modified Twenty ten template files. So it was a llittle unclear if and where i should be adding the functions you talked about above. any help would be greatly appreciated.

Cheers
John

Reply

Matt Dunn September 19, 2010 at 2:29 pm

Sorry, John, but I’m not sure why that would be happening. You’re putting the jQuery in header.php above the closing head tags?

Reply

John MacKenzie September 27, 2010 at 6:56 pm

yes It is above the closing head tag. will i tried that, then I put it higher up in the head tag and that made no difference. I will give it a run thorugh again to see what might be the problem. are you able to detail more clearly what items I should be adding if not using the thesis template?

Reply

John MacKenzie September 27, 2010 at 7:11 pm

ok the line of code it seems to have a problem with is

$(document).ready(function() {

which calls the function so I will check into the function settings and see if maybe i messed up there.

Reply

John MacKenzie September 29, 2010 at 12:29 am

well i am not getting the error anymore (i removed some of the function coding i guess i didnt need to add) problem now is nothing happens, its like jquery is not loading but I have the tags in there and even a direct call to it?? any ideas why Jquery would not run?

Reply

John MacKenzie September 30, 2010 at 6:48 pm

Ok well i got it working!

1) I put this code BELOW the wp_head call not above it as instructed:

2) I changed the $ to jQuery to prevent conflicts. (I had tried this before but it did NOT work when the code was above the wp_head call)

jQuery(document).ready(function() {
jQuery(‘div.archive:eq(0)> ul’).hide();
jQuery(‘div.archive:eq(0)> h3′).click(function() {
jQuery(this).next().slideToggle(‘fast’);
});
});

Reply

John Wills September 21, 2010 at 8:49 pm

Good stuff! Just what I’ve been looking for.
However…
I have two issues that I cannot resolve.
1. The hide feature is not working
2. The Popular Posts is not working. I get “”
You will notice that it works as a widget in the sidebar but not on the archives page.

I’m using Thesis.

I’ve checked as thoroughly as I know how with Firebug and looked through all my custom files but can’t find anything that I think would cause these issues. Do you have any ideas?

Thanks for the great work. I couldn’t do this without people like you who are willing to share your knowledge.

Reply

John Wills September 24, 2010 at 6:51 am

Figured out the Popular Posts problem. Changed line 76 from “<?php popular_posts(); ?>” to “” – still have not figured out the hide feature. Thanks

Reply

John Wills September 24, 2010 at 8:24 am

Got the hide function working.
In addition to turning on jQuery: Thesis Design Options/JavaScript/Sitewide JS Libraries | jQuery
I pasted the script code from step 4. Add the jQuery “for a regular WordPress theme” into: Thesis Design Options/JavaScript/Embedded Scripts
I obviously have no idea why it now works but thought I’d pass it along.
Again, thanks for the work.

Reply

Martyn Chamberlin December 17, 2010 at 10:35 pm

Could you explain how you did you own archive page, Matt? I want to be able to have each month visible, and when you click on a month the individual posts drop down via jQuery.

Here’s what my archive page looks like so far: http://artistsdiscuss.com/archives/

Thanks for your tutorial! Like John Wills, I had to add the JavaScript in the Embedded scripts to get iQuery to work. Never used it before. Pretty cool.

Reply

Stephanie March 13, 2011 at 1:17 pm

Hey – Love this so much and thanks for sharing!

I have been trying to add this to my site but it’s not looking right – even some code showing through on my archives page. I was hoping you could take a look and help me sort through it.

I’m also not able to get the dropdowns to function – my page is just one big list of old post links. What am I doing wrong?

Reply

Lijin April 23, 2011 at 9:57 pm

Hi,

Followed all the steps as provided, but resulted in no jquery animation in page and giving me a error for popular post (Even after popular post from Rob Marsh’s)

Appreciate your help plese

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: