1

How it is possible to display only the parent category of the post. It contains two sub categories and I want to eliminate it.

Example: myblog.com/parent_category/post_name

This is only what I want.

  • There's a great post on how to do the opposite (remove parent, only show child category), which surely must contain the answer if you're capable but I don't have the WP/PHP-fu to edit this to a parent-only function. Any ideas? I'm also hoping to do this for my WP site. – t-jam Oct 8 '12 at 0:47
2

You can try out this code here using post_link filter hook.

This filter is applied to the permalink URL for a post prior to returning the processed url by the function get_permalink. Now we are modifying that to suit our requirement i.e, to display only parent category for the child categories and then followed by the post name.

if ( ! is_admin() ){
 add_filter( 'post_link', 'custom_permalink', 10, 3 );
}
function custom_permalink( $permalink, $post, $leavename ) {
  // Get the categories for the post
  $category = get_the_category($post->ID);
  $parent_category = get_category($category[0]->parent);
  if (  !empty($category) && $category[0]->parent == "271" ) {
    $permalink = trailingslashit( site_url('/'.$parent_category->slug.'/'.$post->post_name.'/' ) );
    return $permalink;
  }else{
    return $permalink;
  }
}
  • Sure, the code here is using 'post_link' filter hook. This filter is applied to the permalink URL for a post prior to returning the processed url by the function get_permalink. Now we are modifying that to suit our requirement i.e, to display only parent category for the child categories and then followed by the post name. – Adarsh Surania Oct 27 '16 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.