nico_lrx
nico_lrx

Reputation: 290

Wordpress: remove one category in permaling /%category%/%postname%/

I am looking for a solution in order to remove /%category%/ from my permalinks for only one category ("general").

My permalink is currently set to /%category%/%postname%/. How can I create the following URLs only for the "general" category /%postname%/ ?

Thanks!

Upvotes: 0

Views: 71

Answers (2)

This works for me:

  function remove_uncategorized( $permalink, $post, $leavename ) {
  if( $post->post_type != 'post' ) return $permalink;
  $cats = get_the_category($post->ID);
  if( ! count($cats) ) return $permalink;

  usort($cats, '_usort_terms_by_ID');
  $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );

  $category_object = get_term( $category_object, 'category' );

  return _clear_uncategorized($category_object, $permalink);
}

function _clear_uncategorized($cat, $permalink) {
  if( $cat->slug == 'sin-categoria' ) {
    return str_replace('%category%/', '', $permalink);
  }
  $parent = $cat->parent;
  if ( !$parent )
    return $permalink;
  return _clear_uncategorized($parent, $permalink);
}

add_filter( 'pre_post_link', 'remove_uncategorized', 9, 3 );

Upvotes: 0

nazg
nazg

Reputation: 71

You can set custom structure in Permalinks Settings: Dashboard - Settings>Permalinks

Upvotes: 0

Related Questions