How To: Build a Categories and Archives Drop-down Box
Over the past couple years I’ve really enjoyed monitoring trends in the blogosphere and one of the trends that has come up recently is blogger’s cleaning up their sidebars by adding drop-down boxes.
If you’d like to build drop-down boxes for your categories and archives on your WordPress blog, here is the code you need:
Archives Drop-down Code
<select name=\"archive-dropdown\" onChange='document.location.href=this.options[this.selectedIndex].value;’>
<option value=\”\”><?php echo attribute_escape(__(’Select Month’)); ?></option>
<?php wp_get_archives(’type=monthly&format=option&show_post_count=1′); ?> </select>
Categories Drop-down Code
<form action="<?php bloginfo('url'); ?>/" method="get">
<?php
$select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
$select = preg_replace("#<select([^>]*)>#”, “<select$1 onchange=’return this.form.submit()’>”, $select); echo $select; ?>
<noscript><input type=”submit” value=”View” /></noscript>
</form>
I think something like this can be a good idea if done with the right theme, but I have also seen it on a few sites where it didn’t look very good, so keep that in mind if you decide to move your categories and archives to a drop-down box!
To see other code snippets we’ve featured here over the past year, check out our WordPress Code page!
How to: Create an Archive Page for your WordPress blog
In my opinion, all blogs should have an archive page: It allows your readers to quickly browse your blog and find what they’re looking for, and this page is also very good for SEO.
Here’s how to create this page on a Wordpress blog:
Archive Wordpress Plugins
There’s many plugins which allows you to automatically create an archive page. The good thing is that you’ll have (almost) nothing to do, and the bad thing is that you will not be able to customize it a lot, or you’ll have to edit the plugin files, which is sometimes a bit too hard if you’re not a developer.
On my blog in French lyxia.org, I use the Smart Archives plugin. Even if it gives me satisfaction, the loading time of the page is very long due to the amount of posts to be displayed simultaneously.
If you want to use a plugin, you shall also give a try to Clean Archives, or Extended Live Archives, which allows numerous personalizations.
Do it yourself
Wordpress allows you to create page templates, so it’s possible to create manually an archive page. This is what I chose to do on my blog in English, CatsWhoCode.com.
Before starting to code, you’ll have to choose between two different kinds of archive page. The first one will list all your posts, and will allow a direct access to every article you wrote. The only bad thing is that when your blog will have many posts, the list may be a bit too long.
The second template, which is better for blogs that have been online since more than one year, will list your posts monthly and by categories.
Your choice is made? So let’s go coding!
First we’ll have to create a new file and name it archives.php. At the beginning of the file, paste the following lines:
<?php /* Template Name: Archive page */ ?>
This php comment define a name for our template, and will later allows us to select it on Wordpress Dashboard, when we’ll create a new page.
First template: Listing all posts
<?php
$posts_to_show = 100; //Max number of articles to display
$debut = 0; //The first article to be displayed
?>
<?php while(have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<ul>
<?php
$myposts = get_posts('numberposts=$posts_to_show&offset=$debut');
foreach($myposts as $post) :
?>
<li><?php the_time('d/m/y') ?>: <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
<?php endwhile; ?>
Second template: Archives by months and categories
<?php while(have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<h2>Categories</h2>
<ul><?php wp_list_cats('sort_column=name&optioncount=1') ?></ul>
<h2>Monthly Archives</h2>
<ul><?php wp_get_archives('type=monthly&show_post_count=1') ?></ul>
<?php endwhile; ?>
After you chose one of the templates I shown you above and pasted it to your archives.php file, you just have to upload it on your wp-content/theme/yourtheme/ directory.
Then, in Wordpress dashboard, create a new page, name it “Archives” (or whatever you want) and select Archive page as page template.
That’s all! You now have an archive page, which is good for both your reader and search engines crawlers.
How To: Blocking Your WordPress Categories and Archives From Google
We all know that duplicate content can be a problem. People copy your work, re-post it on their website, then you both are penalized for duplicate content! Unfortunately, there isn’t much that can be done about that, but did you know that often blogs have duplicate content within their own blog? The biggest culprit for duplicate internal content is your archives page, which is usually used for categories and monthly archives. Unless you only display partial posts in your archives, you’ll want to make sure Google doesn’t index it. If you aren’t handy with Robots.txt, you can instead use this code to easily tell Google not to index your archive.php page.
<?php if(is_archive()){ ?><meta name="robots" content="noindex"><?php } ?>
You’ll want to grab that code and paste it anywhere in the header of your theme above the closing of the head tag. That way, Google will not index these, and search engines won’t refer traffic to your archive pages instead of your single post pages.















