[wp-hackers] category URL's and SEO ranking
Mike Little
wordpress at zed1.com
Sun Jun 17 10:15:20 UTC 2012
There are a number of ways to solve this problem, which, by the way,
doesn't merit such strong words as "hateful" and "punishes". Google's
ranking algorithm is just that: an algorithm, it doesn't hate or punish.
Anyway, there is a filter 'term_link' which WordPress passes the generated
term link and the elements used to construct it. So you can reconstruct it
in the way that you want.
For example, add this code to your theme's functions.php or a plugin
add_filter( "term_link", 'my_term_link', 10, 3 );
function my_term_link( $termlink, $term, $taxonomy) {
// check this is a category link, and we are using permalinks
if ( ( 'category' == $taxonomy ) && ( false === strpos( $termlink, '?' ) )
) {
global $wp_rewrite;
$termlink = $wp_rewrite->get_extra_permastruct( $taxonomy );
$termlink = str_replace( "%$taxonomy%", $term->slug, $termlink );
$termlink = home_url( user_trailingslashit( $termlink, 'category' ) );
}
return $termlink;
}
This will work for category links in the custom menus too (assuming you
don't use custom links there)
Another solution (which feels like a better solution to me) would be to add
a canonical link to the header of the category archives page. This code
will do that.
add_action( 'wp_head', 'category_rel_canonical' );
function category_rel_canonical() {
if ( !is_archive() )
return;
global $wp_the_query;
$term = $wp_the_query->get_queried_object();
$taxonomy = $term->taxonomy;
$t = get_taxonomy($taxonomy);
if ( $t->rewrite['hierarchical'] ) {
$link = get_term_link( $term, $taxonomy );
echo "<link rel='canonical' href='$link' />\n";
}
}
If you run the two lots of code together then you will always get the
non-hierarchical canonical link. But then you might as well not use
hierarchical categories at all.
Mike
--
Mike Little
http://zed1.com/
More information about the wp-hackers
mailing list