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?
Solutions
You can try using
get_the_category()
:
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
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');
The oldest but fastest way you can use is:
$cat_id = get_query_var('cat');
Similar questions
Remove Active Class from Parent when Child is Active in Wordpress SubMenu
I have a menu with secondary items in Wordpress. It works great except the active class should only be on the child when that page is active. Right now it is active on both. What is the best way to remove it? jQuery, Filter, or Editing the Walker. This is Bootstrap based theme so it is using the bootstrap nav walker from GitHub (wp-bootstrap/wp-boo...
WordPress Category, Child Category, Active Category Problems
`Let's say I went to a category page. There is some content on this category page. I would like to show all the child categories of the parent category of this category below the content or under this category page. I would also like to show the description and thumbnail image of the category. How do I do that? Then if a user goes to a category pag...
Display current category without an active link in wp_list_categories
I appreciate your time in looking into this for me. I'm using wp_list_categories tag to display a list of categories in my navigation menu and I'd like to disable the link to the current category when viewing said category page. My main reason is that the color attribute in my CSS style for li.cat-item a is overriding the color attribute for my CSS...
Add active class to woocommerce product category current-menu-item
I've used the below code to add the active class to menu items. The site is 99% Ecomm using Woocommerce and I'm using product categories as top level navigation items. The below code works with almost all items except when being active on a product sub-category.
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
...
Also ask