Support » Fixing WordPress » Get Current Category ID

  • I have gone through every function to accomplish this with no luck. It seems that this function was deprecated. Can someone please give me the correct function to call the CURRENT CATEGORY ID.

    The following do not work:

    global $post;
    $categories = get_the_category($post->ID);
    if (($actcat) && (is_single())) {
    global $wp_query;
    $cats = '';
    foreach (get_the_category($wp_query->post->ID) as $catt) {
    $cats .= $catt->cat_ID.' ';
    $cats = str_replace(" ", ",", trim($cats));
    $cat_rows = $wpdb->get_results("SELECT term_id FROM wp_term_taxonomy WHERE taxonomy = 'category'");
    	foreach ($cat_rows as $cat_row) {
    		if( is_category( $cat_row->term_id ) ){
    			$current_cat_wtf = $cat_row->term_id;
    $category = get_queried_object();
Viewing 1 replies (of 1 total)
  • Hi webmdt,

    If you use the function get_term_by you’ll be able to get what you want –
    The code below should do what your’re after.

    $albumName = single_cat_title('', false); // set current category name as a variable
    $albumMeta = get_term_by('name', $albumName, 'taxonomySlug'); // get the meta from current cat
    echo print_r($albumMeta); // print the returned array to see what you now have at your disposal

    Happy coding 🙂

Viewing 1 replies (of 1 total)
  • The topic ‘Get Current Category ID’ is closed to new replies.