Powered by Blogger.

Get the Category Id and Title in wordpress


Display the category name:
<?php
$category = get_the_category(); 
echo $category[0]->cat_name;
?>
But, If multiple categories, it will show only the first category name.
single_cat_title(), it's working only in category.php for me.
<?php $current_category = single_cat_title("", false); ?>
false is used when we need to assign value in PHP variable,
<?php $current_category = single_cat_title("", true); ?>
true is used when we need to display category without echo, the first parameter is for prefix for the category, the first parameter is only working for a true option for me.
Get the category Id:
<?php echo $cur_cat_id = get_cat_id( single_cat_title("",false)); ?>

No comments