WordPress categories, optimization tips and custom pages

It’s not a SEO secret, inner links to / from strong category pages will help your site to rank higher in Google. Sad enough that WordPress doesn’t provide a lot of functionality for these important sections on your blog site. This is the information you can read below the field “description” if you edit a category:

The description is not prominent by default, however some themes may show it.

There is no field for a second title and the description field doesn’t allow most of the HTML elements. I’m using the All in One SEO plugin because it gives me the freedom to write a custom page title or meta description (for posts or pages only). Most SEO plugins doesn’t care a lot about category pages…

First preparations

In my experience it’s the best to not index any archive page created by WordPress because they show content duplicates. The category URL could need an update first: By default you get this kind of category URL:

http://www.domain.com/archives/category/some-category-name

We need to remove the “archives” part from the URL, goto Settings->Permalinks and add some unique name for the field “Category base”.

If you use the “All in One SEO” like I do, disable the following settings:
Canonical URLs, Use noindex for Categories

Head to the plugin category, open the file (aioseop.class.php) and disable (//) row 283 to disable the creation of a meta description for all category pages:

//$description = $this->internationalize(category_description());

This function is buggy because the auto generate option will use the complete category text for a meta description.

We will create for the category pages our own META information and the canonical URLs for posts and pages are created by WordPress since version 2.9. Next you need to create some unique text for all the categories, 100 words is a great text length.

Optimizations for the “generated” category pages

We need to create some code to get the right META data in our header section. In the theme directory is a file called “functions.php”, open the file in a text editor and add this code:

function create_short_version($text, $len = 150) {
	$parts = explode(' ', $text);
	$ic = count($parts);
	$txt = '';
	for ($i = 0; $i < $ic; $i++) {
		$txt .= $parts[$i].' ';
		if (strlen($txt) >= $len) break;
	}
	$txt = trim($txt).'...';
	return $txt;
}
 
function fix_meta_canonical() {
	global $paged;
	if (is_category()) {
		$catid = get_query_var('cat');
		$catdescr = strip_tags(trim(category_description($catid)));
		if (strlen($catdescr) > 160) {
			$meta = create_short_version($catdescr);
		} else {
			$meta = $catdescr;
		}
		echo '
<link rel="canonical" href="'.get_category_link($catid).'" />
<meta name="description" content="'.$meta.'" />';
		if ($paged > 1) echo '
<meta name="robots" content="noindex,follow" />';
	} else {
		echo '';
	}
}
add_action('wp_head', 'fix_meta_canonical');

The first function “create_short_version()” is a helper which creates a short version from a text without changing the last word into something unreadable. Inside the function “fix_meta_canonical()” we create additional META information if the current page is a category. We don’t allow search engines to index other category pages than the first page.

Now we need to change our default theme file. Open the file “archive.php” (or category.php) and add the following code right below the header element and before the loop:

	<?php 
	if (is_category() && $paged < 2) {
		echo '
		<p>'.category_description().'</p>';
	} 
	?>

Using this code the category page will show the description as well, but only on the first page!

Optimize your most important categories

I use for most of my blog posts more than one category, for example this article is listed in the categories “PHP Scripts” and “Search engine optimization”. If you analyze your blog you will see that some category is more important or is used more often. While the steps above are okay for most the categories, it might be better to create a custom or static page for some of your categories. We did this for the category PHP Scripts.

With WordPress it’s possible to create a static page for just a single category. How does it work? Create a copy of the “archive.php” file and call it category-XX.php where XX is the category ID from the category you like to create a static page for. You can find the ID in the browser’s address bar if you edit a category. Add your custom code and the category description to this newly created file and save your work. I use the static content only for the first page and show the regular code for all underlying pages:

<?php 
if (is_category() && $paged < 2) { 
    // show here your custom code/content
} else {
    // show here the code from your archive.php file
}
?>

Don’t remove any code from outside the DIV container with the ID “content”. This modifications are only for the “information part” and about the design or the layout.

Header and title improvements

For SEO reasons it’s much better to have different titles in the page title and your header element. With the following code it’s possible to tweak the page title as well:

// create here your custom titles
$my_pagetitels = array(
	4 => 'PHP scripts and tutorials'
);
 
function change_cptitle() {
	global $my_pagetitels;
	$catid = get_query_var('cat');
	if (array_key_exists($catid, $my_pagetitels)) {
		$title = $my_pagetitels[$catid]; // use a custom title
    } else {
    	$title = get_the_category_by_ID($catid); // use the category name as title
    }
    return $title;
}
add_filter("single_cat_title", "change_cptitle");

Sure this solution is very quick and dirty, but it works :) Add your custom titles within the array which is called “$my_pagetitels”. Use the category ID as array index number and enter the “page title” as value. The function will check the array for an existing value and will use the category name as alternative. With a static value for your header element you use three different values instead of only one (the category name).

Don’t forget to add the category pages to you’re Google Sitemap. Remove the categories from your “robots.txt” file if you blocked them before.

That’s all, for some of the tweaks you need to edit several WP theme files to add or change a category. I’m sure it’s worth the time and work! Don’t forget to create some external links to your category pages! If you have any questions or suggestion, you’re welcome to post them below.

Comments

  1. Hi Olaf,

    Great tutorial. I found it exceptionally frustrating trying to set up my latest WordPress site. My concept makes heavy use of category pages and it seems the WordPress community is still not seeing the true potential for category pages as content and gateway pages.

    Quite a few of the plugins that are out there for working with category pages also do not appear to work right with WP 3.0. I suppose with any new version the plugins lag behind a bit but it seems like every category related plugin has problems with the new version.

    Thank you for such an easy to understand tutorial!

  2. It helps your SEO a lot if you use category pages on your site, don’t forget to link those category pages from your detail pages too!

  3. Yes right, breadcrumbs can help here too. The best thing is to have categories and sub-categories.

  4. Thanks Michele, at this moment WordPress categories have almost the same function as tags. There is only one difference: you can create static category pages

  5. Does someone know a plugin which make makes it possible to pass html code to my category text? Which SEO plugin has good support for category pages?

  6. For SEO: the “All in one SEO” doesn’t work well for categories, you can try:
    http://wordpress.org/extend/plugins/headspace2/

  7. Good tutorial, I would still allow robots to crawl all my category pages and have the description appear on the first page only.

  8. We allow Google to crawl all pages, the only think you shouldn’t allow Google is to index them (means they doesn’t appear in the search results).
    It’s make no sense to add duplicate pages to the google index and in fact a category result page is just a (non-unique) summary of your unique blog posts. Check in Google Analytics the traffic for your “paged” content and check the traffic sources, I’m sure that the organic traffic is zero.

  9. Hello.
    Does it pay off to optimize category pages?

    I mean, have you noticed any significant change in terms of visits and google rankings?

  10. Hi,
    difficult to say because I changed the site before a period which provides less traffic. If I check the organic traffic for this blog, this part is still strong (as it was before).

    I’m sure that it helps and you should use them, it’s good for your SEO.

  11. Hi Olaf,

    I’ve been going mad trying to find a way to add custom category page titles, think I may have found it. Being a complete php newb, where exactly would I add the page titles and category ID’s?

    Wes

  12. Also, where do I place the code for the custom page titles?

    – Wes

  13. Hey Wes,

    You can create custom category pages using this naming conventions, just in case your category is “8” your name looks like: category-9.php

    You can find the category ID in the url, if you edit one.
    the page titles is tricky, check the part “Header and title improvements”. This way the title is generated by the WordPress application.

  14. Hi Olaf,

    Does the code given in ‘Header and title improvements’ relate to only custom created category pages, or can you add this code for your generic category pages, ie. the default category pages from WP?

    You see, the default page titles for category pages are massively limited, with just %category name% | %blog title% for example. I want to be able to add custom titles per category page, not be restricted by WP’s failure to offer any flexibility.

    Hoping this could solve my problem.

    Many thanks!
    – Wes

  15. Hi Wes,

    yes you can use that function for “generic” category pages too.
    Just create for each used category ID and entry inside the array called “$my_pagetitels”.
    Copy the array and the function into your templates “functions.php” file and the titles are changed.

    The tutorial doesn’t have a solution for custom headers/titles on generic category pages :(

  16. Hi Olaf,

    That seems contradicting to me. I might have read it wrong but you’ve just said that “yes you can use that function for “generic” category pages too.”

    and then on the last line you said:

    “The tutorial doesn’t have a solution for custom headers/titles on generic category pages :(”

    So it can be used but it can’t? lol I don’t get it.

    – Wes

  17. Using the function you can create custom page titles (head part) and not headers (body part) ;)

  18. Ah right, got ya that time! ;)

    Cheers,
    – Wes

  19. You’re welcome :)

  20. Hi Olaf,

    I’ve added the code you suggested in the ‘header and title improvements’ section, but it’s making no changes to the title. I’ve tested on one category only with ID 47 which is this page http://www.muscle-gain-tips.com/bbq-grilled-recipes

    Any ideas?

    Many thanks,
    – Wes

  21. Hi Wes,

    if you open your header.php template file, what code is used to create the page title?
    Do you use any plugin that has modified the page title as well?

  22. you need to put code between the “code” tags and remove the php open/close tags because WordPress filters those :(
    I use the seo plugin too, do you use the default settings for the title (within the SEO plugin)?

  23. Hi Olaf,

    The code for the title is:

    “”

    The plugin i’m using for all the site page titles is All in One SEO.

    – Wes

  24. Hello Wes,

    it seems that some theme header doesn’t use the same code for the page title. To get the code above working you need to use the function called “single_cat_title()” inside the title tag. I use the following code in my theme:


    < ?php
    if (is_home () ) {
    bloginfo('name');
    } elseif ( is_category() ) {
    single_cat_title();
    echo ' - ' ; bloginfo('name');
    } elseif (is_single() ) {
    single_post_title();
    } elseif (is_page() ) {
    single_post_title();
    } else {
    wp_title('',true);
    } ?>

  25. you need to replace that code:
    < ?php wp_title( '|', true, 'right' ); ?>

  26. Hi Olaf,

    Is that in place of the code that’s already there or?

    Cheers,
    – Wes

  27. Ok, and that won’t affect all my page titles that go through all in one seo plugin?

    – Wes

  28. Actually your current setting is less flexible, the seo plugin will overrule those settings (if used).

  29. Wes, the seo plugin is not related. It’s just if you write custom POST titles, the code above is able to show these titles too. Just replace the title code and the categories should be modified according the changes you made inside the functions.php file.

  30. Hi Olaf,

    What settings do you have the all in one seo plugin set at? It’s imperative the seo plugin still works on all other pages. If I can’t then the code is useless.

    – Wes

  31. Ah, understood.

    The custom titles are now showing, thanks a lot Olaf. Really useful bit of code. A plugin would be great ;)

    – Wes

  32. Hi again Olaf,

    I’ve started to add more titles to the code, now i’m getting the following error on the second line of code/page title:

    Parse error: syntax error, unexpected T_LNUMBER, expecting ‘)’ in /home/weigh8/public_html/muscle-gain-tips/wp-content/themes/muscle/functions.php on line 581

    Here’s the code as it stands:

    $my_pagetitels = array(
    6 => ‘Muscle Building Blog | Muscle Gain Tips’
    11=> ‘Muscle Building Lunch Recipes’
    );

    function change_cptitle() {
    global $my_pagetitels;
    $catid = get_query_var(‘cat’);
    if (array_key_exists($catid, $my_pagetitels)) {
    $title = $my_pagetitels[$catid]; // use a custom title
    } else {
    $title = get_the_category_by_ID($catid); // use the category name as title
    }
    return $title;
    }
    add_filter(“single_cat_title”, “change_cptitle”);

  33. you need to seperate the array elements with a comma:


    $my_pagetitels = array(
    6 => 'Muscle Building Blog | Muscle Gain Tips', // check the comma left from here
    11=> 'Muscle Building Lunch Recipes'
    );

  34. I want to know when category pages and tags created duplicates and you say they should not be indexed why then all the big news sites and popular blogs index all of their tags?
    I can name http://technorati.com/blogs/top100 as an example, why their ranking in Google is good, is it useful for them too?
    Yours Sincerely

  35. Hi Javid,
    removing duplicated or generated content from your site is a basic SEO stratgy ;)
    The sites you have mentioned rank good because of their authority and the great and unique content they offer. If you get so many links, it’s maybe less important to have unique content for categories and tags. Techchrunch has unique content for most of the tags and companies mentioned/linked inside their articles.

    Don’t forget that my article is a little bit outdated, f.e. I would never use “All in one SEO” these days…

Because of all the spam attemps I've decided to close the comment form at this time. If you have have any questions or comments please post them by using Google+ or Twitter (the links to my profiles are located at the top of this page).