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?
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.
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.
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.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With