WordPress.org

Ready to get started?Download WordPress

Forums

Get Current Category ID (2 posts)

  1. webmdt
    Member
    Posted 1 year ago #

    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:

    get_query_var('cat');
    global $post;
    $categories = get_the_category($post->ID);
    var_dump($categories);
    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();
  2. Dan Norris
    Member
    Posted 10 months ago #

    Hi webmdt,

    If you use the function get_term_by you'll be able to get what you want - http://codex.wordpress.org/Function_Reference/get_term_by
    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic