Wordpress

Get the parent category of post in wordpress

get current post id

$id = get_the_ID();
echo $id;


get current post parent category slug

$category = get_the_category();
$category_parent_id = $category[0]->category_parent;
if ( $category_parent_id != 0 ) {
$category_parent = get_term( $category_parent_id, ‘category’ );
$cat_parent_slug = $category_parent->slug;

} else {
$cat_parent _slug = $category[0]->slug;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

13 + twelve =