Unable to get current Category ID in category page

Posted in: Newspaper
Post count: 30

I’m creating a shortcode to use in both posts and categories.

To make this shortcode work I just need to get the category ID.

If I’m in a post it works fine, but if I’m in a category there doesn’t seem to be a way to access the current category ID.

If I try to debug it I see that the shortcode inserted in a category template (cloud template) doesn’t recognize is_tax() or is_category()… but always falls under is_single().

is (is_tax()) {
// Don't work
}
elseif (is_category()) {
// Don't work
}
elseif (is_single()) {
// Single Post => Works well
if (get_post_type() === 'post') {
$post_id = get_the_ID();
$post_title = get_the_title($post_id);
$categories = get_the_category($post_id);
if (!empty($categories)) {
$category_id = $categories[0]->term_id;
}
} elseif (get_post_type() === 'tdb_templates') {

// This is the point!
// How do I get the current category ID?

}
}

Can you help me?

  • This topic was modified 1 year by ikiweb.
  • This topic was modified 1 year by ikiweb.
Post count: 30

I solved it thanks to this code:

$tdbLoadDataFromId = tdb_state_content::get_wp_query()->queried_object_id;
$current_category_obj = get_category($tdbLoadDataFromId);
if ($current_category_obj) $category_id = $current_category_obj->cat_ID;

Viewing 2 posts - 1 through 2 (of 2 total)
The forum ‘Newspaper’ is closed to new topics and replies.