WordPress.org

Support

Support » Plugins and Hacks » Hacks » Get the ID of the current category in taxonomy.php

Get the ID of the current category in taxonomy.php

Viewing 5 replies - 1 through 5 (of 5 total)
  • You can get the category id by the following code:

    <?php
    $catId = get_the_category( $post->ID );
    ?>

    For more reference: http://codex.wordpress.org/Function_Reference/get_the_category

    I think what MarkedUp needs is the ID of the category being displayed on an archive page, not the categories assigned to a particular post (in which case using get_the_category is right).

    To get the ID being displayed, use the following:

    $wp_query->get_queried_object_id();

    On a post page it returns the post ID, on a author page it returns the author ID, and on a taxonomy page it returns the taxonomy ID. 🙂

    You can use below code and give a try :-

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]

    <?php
    $termID = 10;
    $taxonomyName = "products";
    $termchildren = get_term_children( $termID, $taxonomyName );
    
    echo '
    <ul>';
    foreach ($termchildren as $child) {
        $term = get_term_by( 'id', $child, $taxonomyName );
        echo '
    <li><a>name, $taxonomyName ) . '">' . $term->name . '</a></li>
    ';
    }
    echo '</ul>
    ';
    ?>
    Moderator Jan Dembowski

    @jdembowski

    Brute Squad and Volunteer Moderator

    @pwhstacy? We all appreciate the help, but you really need to learn to use the code button when you post code. 😉

    http://codex.wordpress.org/Forum_Welcome#Posting_Code

    @vincent Ballut

    Excellent Reply!! Thanks

    $wp_query->get_queried_object_id();

    Return Custom Taxonomy Tag ID.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get the ID of the current category in taxonomy.php’ is closed to new replies.
Skip to toolbar