WordPress – customize category and tag links

In case you need to create a custom link for category or tag, the you can term_link filter. This can be useful in case you want to create a destination page for some specific tag or category. Here is an example code for achieving it.

add_filter('term_link', 'term_link_filter', 10, 3);
function term_link_filter( $url, $term, $taxonomy ) {
  $newurl = $url;
  if ($taxonomy == 'category') {
    $newurl = str_replace("/category/foo/", "/foo/", $newurl);
  } else if ($taxonomy == 'post_tag') {
    $newurl = str_replace("/tag/bar/", "/bar/", $newurl);
  }
  return $newurl;
}

Note that this will only change the category/tag link appearing on posts, etc. You will still need to implement the new page. The old category and tag urls will still work. Just the links on posts, etc. will be replaced.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments