[wp-hackers] category URL's and SEO ranking

Haluk Karamete halukkaramete at gmail.com
Sun Jun 17 13:18:54 UTC 2012


Mike,

Thank you for the code. I have only a handful of cats, ( 20 so to speak ).
And not all of them needs this kind o shortening. For example, a URL like

site.com/explore/region/north-america

, though longer, is better than

site.com/explore/north-america.

Ofcourse, what's better or not is debatable but the point is for some
cats, the site owner decides to go with the full url and in some the
shorter one.

Having said that, would then the following adjustment to your code be
OK to go with;

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,
'?' ) ) )
    {
        if(0):
            global $wp_rewrite;
            $termlink = $wp_rewrite->get_extra_permastruct( $taxonomy );
            $termlink = str_replace( "%$taxonomy%", $term->slug, $termlink );
            $termlink = home_url( user_trailingslashit( $termlink,
'category' ) );
        else:
            $taxonomy_term = $taxonomy . "_" . $term;
            switch($taxonomy_term):
            case  "3_10": //alternatively, I can go here
"category_video" too right? Assuming the filter is called like that?
                    //overwrite the WP computed URL alone when
taxonomy ID is 3 and term id is 10
                    $termlink = "/category/video";
                    break;
            default:
                    //leave WP computed URL alone
                    break;
            endswitch;
        endif;
    }
    return $termlink;
}

Also, I could not understand the part " ( false === strpos( $termlink,
'?' ) ) ) "
When you check for the query string existence, what do you make of
that? Does your original code uses the full URL ( that is
/category/multimedia/video as opposed to /category/video ) when the
URL happens to have a QS attached to it? Did I get this part wrong?



On Sun, Jun 17, 2012 at 3:15 AM, Mike Little <wordpress at zed1.com> wrote:
> 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/
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list