0
votes

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.

1
Can you maybe give us a link to see what's the issue?designtocode

1 Answers

1
votes

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