Change how the categories are listed for posts

Most themes have a link to each category in underneath the title of the post on the individual posts.

These links are rarely if ever, styled, and in most cases push “link-juice” toward the category page.

This is not good for SEO, so the best solution is to change the way the categories for the post is displayed.

[warning]

You will have to edit your theme for this, so this is not for beginners.

[/warning]

1. Find and locate your “single.php” file in your theme.

2. Find the following code

<?php the_category(', ') ?>

Now, there are several ways to change it. The first solution is to simply list the categories without having any links to them. That can be done by replacing above code with the following:

<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>

This just lists all categories as a long line.

The second solution is to list the categories as you usually would, but by adding rel=”nofollow” to the links, so you do not pass link-juice to the category pages.

This can be done with the following code:

<?php
$catcount=0;
foreach((get_the_category()) as $category) {
$catcount++;
if ($catcount>1) {echo ', '; }
echo '<a href='.get_category_link($category->cat_ID).' rel='nofollow category tag'>' . $category->cat_name. '</a>';

}
?>

The $catcount variable is a little code I added which displays a comma if there are more than one category. If we did not check for this, the code would simply add a comma every time, even if there was just one category listed.

If you want to use something else such as “and”  instead of the ,”” comma, change the following line:

if ($catcount<1) {echo ', '; }

Example:

if ($catcount>1) {echo 'and '; }

If you are looking for other tips and tricks, check out Setting Featured Image via code or WordPress Multisite Search.

1 Comment

  1. lkoudal (lkoudal) on 23/03/2009 at 16:19

    RT @larskoudal : Change how the categories are listed for posts in WordPress

Leave a Comment





Cleverplugins.com Newsletter

Articles about how to improve your WordPress website, how to get more traffic and most importantly more customers.

Subscribe

Something went wrong. Please check your entries and try again.