microtag
3/7/2017 - 8:25 AM

Trovare category slug per il post corrente From http://stackoverflow.com/questions/21361536/how-to-get-parent-category-slug-of-current-post

//get all categories of current post
$categories = get_the_category($post->ID);

//get top level category of current post
$top_cat_obj = array();
foreach($categories as $cat) {

    if ($cat->parent == 0) {
        $top_cat_obj[] = $cat;  
    }

}           
echo $top_cat_name = $top_cat_obj[0]->name;
echo $top_cat_slug = $top_cat_obj[0]->slug;