ControlledChaos
6/24/2018 - 12:32 AM

Get a category name/slug/id for a post or archive page.

Get a category name/slug/id for a post or archive page.

<?php

if ( is_single() ) {
    $cats =  get_the_category();
    $cat  = $cats[0]; // let's just assume the post has one category
}
else { // category archives
    $cat  = get_category( get_query_var( 'cat' ) );
}

$cat_id   = $cat->cat_ID;
$cat_name = $cat->name;
$cat_slug = $cat->slug;

Get a category name/slug/id for a post or archive page

WordPress Snippet