Get current term’s ID

Im using the following code to get an array of children taxonomies and write them out with links in an unordered list.

    <?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 href="' . get_term_link( $term->name, $taxonomyName ) . '" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">' . $term->name . '</a></li>';
}
echo '</ul>';
?>

What I’m trying to achieve is to get the actual term (category) id so I can replace it on $termID and don’t have to hardcode the id of the term.

Any help would be kindly appreciated!

Thanks!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

To get the Current term you can use get_query_var( 'term' ); and to get the current taxonomy you can use get_query_var( 'taxonomy' ) so you can do something like this:

$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );

echo '<ul>';
foreach ($termchildren as $child) {
    $term = get_term_by( 'id', $child, $taxonomyName );
    echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">' . $term->name . '</a></li>';
}
echo '</ul>';

Method 3

Or you can use : term_exists( $term, $taxonomy, $parent )

$term_id = term_exists( $term_name );

See WordPress Codex

Check if a given term exists and return the term ID

Returns the term ID if no taxonomy was specified and the term exists.

Method 4

To get the current term ID, use:

$term_id = get_queried_object()->term_id;

get_query_var cannot be used in thise case, since term_id is not in the list of vars available publicly.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x