Forums

[resolved] Getting current category ID (3 posts)

  1. gabi83tm
    Member
    Posted 3 years ago #

    Is there an easier way of getting the current category ID? (Without having to access the database)

    <?php
    	$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 ) ){
    			$what_i_am_looking_for = $cat_row->term_id;
    			echo 'The current category ID is: ' . $what_i_am_looking_for;
    		}
    	}
    ?>

    Here I'm getting all the categories from the database and I'm testing whether or not each of them is the current category... not very effective...

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    What do you mean by current?

    I'll guess this might help:
    $cat = get_query_var('cat');

  3. gabi83tm
    Member
    Posted 3 years ago #

    WOW! This was fast.
    It's working. Thanks alot!

Topic Closed

This topic has been closed to new replies.

About this Topic