Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Using /%category%/ %postname%/ for the permalink I get a URL string of all the categories that the specific post is included in. I would like the categories in the url to be filtered down.here it is,

I have a celebrity gossip blog and I have this category structure:

www.myblog.com/category_1 › sub category_1 ›sub category_2 > Post_name
ex: www.myblog.com/pics/a to c/Aniana Lema /myPostName

I would like to skip the "subcategory_1,2" in the URL,(only display the parent category) ex: www.myblog.com/pics/myPostName

Is it possible?

share|improve this question
    
if just skipping "subcategory_1", wouldnt' it be "www.myblog.com/pics/Aniana Lema /myPostName/" ? –  peteroak Aug 4 '12 at 9:53
    
sorry friend; i have corrected it.Anyway thanks –  sheena Aug 4 '12 at 10:56
add comment

1 Answer

strangely familiar to this, but it is different, so here's a modified version

add_filter( 'post_link', 'remove_parent_cats_from_link', 10, 3 );
function remove_parent_cats_from_link( $permalink, $post, $leavename ){

    $cats = get_the_category( $post->ID );
    if ( $cats ) {
        // Make sure we use the same start cat as the permalink generator
        // what happens now actually is the opposite,
        // we end up using the latest category that has a parent
        usort( $cats, '_usort_terms_by_ID' ); // order by ID

        foreach( $cats as $cat ) {

          if ( $cat->parent ) {
              // If there are parent categories, collect them and pick the top most
              $parentcats = explode(" ",get_category_parents( $cat, false, ' ', true ));
              $topcat = $parentcats[0];

          } else {
              $topcat = $cat->slug;
          }
        }
    }
    $permalink = home_url()."/".$topcat."/".$post->post_name;
    return $permalink;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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