Get current term’s ID

Here is a function I use to list subterms:

/**
 * Lists all subentries of a taxonomy.
 *
 * @return void
 */
function ttt_get_subterms( $args = array () )
{
    if ( ! isset ( get_queried_object()->taxonomy ) )
    {
        return;
    }

    $options = array (
        'child_of'           => get_queried_object_id()
    ,   'echo'               => 0
    ,   'taxonomy'           => get_queried_object()->taxonomy
    ,   'title_li'           => FALSE
    ,   'use_desc_for_title' => FALSE
    );

    $settings = array_merge( $options, $args );

    $subtermlist = wp_list_categories( $settings );

    // Without results WP creates a dummy item. It doesn't contain links.
    ! empty ( $subtermlist ) and FALSE !== strpos( $subtermlist, '<a ' )
        and print "<ul class=subterms>$subtermlist</ul>";
}

Use it like wp_list_categories().

Avoid get_term_by(). It is very expensive and not necessary.

Leave a Comment