• Resolved jezternz

    (@jezternz)


    Hi.
    In the Sidebar. I have a dynamic ID, I want the name of the category, how do I get the category name from the id? (outside the loop).
    Thanks in advance, Josh.

Viewing 3 replies - 1 through 3 (of 3 total)
  • This works when on a category query page:

    <?php
    $category = $wp_query->get_queried_object();
    $cat_name = $category->name;
    ?>

    And this should work anywhere else:

    <?php
    $cat_id = $DYNAMIC_ID;
    $categories = get_categories();
    foreach( $categories as $category ) {
    	if( $category->term_id == $cat_id ) {
    		$cat_name = $category->name;
    		break;
    	}
    }
    ?>

    Note you want to change $DYNAMIC_ID in the latter bit of code to the var holding your category (dynamic) ID. In both cases $cat_name will hold the category name.

    Thread Starter jezternz

    (@jezternz)

    Thanks heaps Kafkaesqui. Works Perfectly.

    Thread Starter jezternz

    (@jezternz)

    Thought I may aswell post the function I found that replaces this.
    $catname = get_cat_name($catid);

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WordPress Category ID -> WordPress Category Name’ is closed to new replies.