- This topic is empty.
- AuthorPosts
-
January 12, 2012 at 3:08 am #9529
ronniet
ParticipantLooking 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?January 12, 2012 at 3:47 am #9541ash108
ParticipantYou can try using
get_the_category()
:$categories = get_the_category(); $category_id = $categories[0]->cat_ID;
January 12, 2012 at 9:47 am #9542ram-mehar-deswal
ParticipantIf 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');
May 21, 2013 at 2:16 am #9533bill
ParticipantI found this question whilst looking for exactly what you asked. Unfortunately you have accepted an incorrect answer. For the sake of other people who are trying to achieve what we were trying to achieve, I thought I’d post the correct answer.
$cur_cat = get_cat_ID( single_cat_title("",false) );
As you said
single_term_title("", false);
was correctly returning the category title, I’m not sure why you would have had troubles with your code; but the above code works flawlessly for me.June 18, 2013 at 8:05 am #9540fred-k
ParticipantThe oldest but fastest way you can use is:
$cat_id = get_query_var('cat');
September 22, 2013 at 11:08 am #9538jorge-orpinel-prez
ParticipantI think some of the above may work but using the get_the_category function seems tricky and may give unexpected results.
I think the most direct and simple way to access the cat ID in a category page is:
$wp_query->query_vars['cat']
Cheers
January 10, 2014 at 11:00 am #9539shaffe
ParticipantI use the get_queried_object function to get the current category on a category.php template page.
$current_category = get_queried_object();
Jordan Eldredge is right, get_the_category is not suitable here.
February 4, 2015 at 12:28 pm #9534bheru-lal-lohar
ParticipantAlternative –
$catID = the_category_ID($echo=false);
EDIT: Above function is deprecated please use
get_the_category()
August 9, 2016 at 5:02 am #9535nh-labs
ParticipantI used this for breadcrums in the category template page:
$cat_obj = $wp_query->get_queried_object(); $thiscat_id = $cat_obj->term_id; $thiscat = get_category($thiscat_id); $parentcat = get_category($thiscat->parent);
February 10, 2017 at 4:43 am #9530abhijit-patel
Participant$cats = wp_get_post_terms( $post->ID, 'product_cat' ); foreach($cats as $cat){ /*check for category having parent or not except category id=1 which is wordpress default category (Uncategorized)*/ if($cat->parent != '0' && $cat->term_id != 1){ echo '<h2 class="link"><a href="'.get_category_link($cat->term_id ).'">'.$cat->name.'</a></h2>'; break; } }
January 22, 2019 at 6:11 am #9531robert-sinclair
ParticipantTried above for solutions to find cat ID of a post, but nothing worked, used the following instead:
$obj = get_queried_object(); $c_id = wp_get_post_categories($obj->ID);
March 12, 2020 at 4:30 am #9536honk31
Participantif you need the category ID, you would get it via get_query_var, that is capable of retrieving all publicly queryble variables.
$category_id = get_query_var('cat');
here is an example to get the category name
$category_name = get_query_var('category_name');
and of course the all mighty get_queried_object
$queried_object = get_queried_object();
that is returning the complete taxonomy term object (when used on a taxonomy-archive page..)
September 16, 2021 at 9:54 am #9532hemant-adhikari
ParticipantHere’s an efficient method to get category meta data, along with HTML if you want to print it out on the front-end:
<?php function custom_get_categories() { $categories = get_the_category(); $uncategorised_id = get_cat_ID('Uncategorized'); $custom_category_link = ''; foreach ($categories as $category) { if($category->category_parent == $uncategorised_id || $category->cat_ID == $uncategorised_id) { continue; } $custom_category_link = get_category_link($category->cat_ID); ?> <a href ="<?php echo $custom_category_link ?>"> <?php echo "\n\nCategory ID: " . $category->cat_ID . "\nCategory Name" . $category->name; ?> </a> <?php }`` } ?>
January 15, 2022 at 1:00 am #9537sangeet kukreja
ParticipantI tested all these answers and this is the only one which works on all archive page types including posts page.
$category_id = get_the_category( get_the_ID()); $cat_id = get_category($category_id[0]->term_id);
- AuthorPosts
- You must be logged in to reply to this topic.