Reputation: 2193
I have been looking for this function everywhere but cannot seem to find a clear way to do it.
I have created a blog with a category-based menu but I was wondering how I could get the menu item to display its category as a title whenever it is clicked. I need this because on my website I have post items which share the same category therefore I need to be able to tell the user of his/her location. Once the user clicks on the post however, it is the first category which will be displayed as a title and this seems to be working with:
$category = get_the_category();
echo $category[0]->cat_name;
The only time that this is a problem is when the posts that share the categories do not have their main category listed first.
I hope that I am making myself clear,
Thanks.
Upvotes: 0
Views: 853
Reputation: 2513
You can look at the category slug in the URL, and get the category by its slug.
$cat = get_category_by_slug( $slug );
echo $cat->name;
http://codex.wordpress.org/Function_Reference/get_category_by_slug
Upvotes: 1