Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current category ID of the active page

Looking to pull the category ID of a specific page in WordPress that is listing all posts using that specific category. Tried the below but not working. I am able to get the category name using single_term_title.

$category = single_term_title("", false); $catid = get_cat_ID( $category ); 

$category is displaying "Entertainment" for example. But I also need the ID of "Entertainment". How would I go about this?

like image 311
RonnieT Avatar asked Jan 12 '12 03:01

RonnieT


People also ask

How do I find category ID?

Simply open a category to edit, and you'll see the category ID in the browser's address bar. It is the same URL that appeared when there was a mouse hover on your category title. It means that the category ID is the number between 'category&tag_ID=' and '&post_type', which is 2.

How do I find the current category ID in WordPress?

Get Current Category ID$category = get_queried_object(); echo $category->term_id; Just place that code in any template file where a category has been queried, such as category archive pages, and you will be able to get the category id no problem.

How can I get current page category in WordPress?

To fetch the post category, you need to use something called as get_the_category() function. $the_cat = get_the_category(); This function returns the current post category if you use it inside a loop.


1 Answers

If it is a category page,you can get id of current category by:

$category = get_category( get_query_var( 'cat' ) ); $cat_id = $category->cat_ID; 

If you want to get category id of any particular category on any page, try using :

$category_id = get_cat_ID('Category Name'); 
like image 128
Ram Mehar Deswal Avatar answered Sep 22 '22 08:09

Ram Mehar Deswal