WordPress.org

Ready to get started?Download WordPress

Forums

Get the ID of the current category in taxonomy.php (6 posts)

  1. MarkedUp
    Member
    Posted 1 year ago #

    When viewing a category using taxonomy.php, how do I get the ID of that category being displayed?

  2. Chandan Kumar
    Member
    Posted 1 year ago #

    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

  3. Vincent Ballut
    Member
    Posted 1 year ago #

    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. :)

  4. pwhstacy
    Member
    Posted 1 year ago #

    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>
    ';
    ?>
  5. @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

  6. Samrat Saha
    Member
    Posted 1 year ago #

    @Vincent Ballut

    Excellent Reply!! Thanks

    $wp_query->get_queried_object_id();

    Return Custom Taxonomy Tag ID.

Topic Closed

This topic has been closed to new replies.

About this Topic