Return only the custom sub-term for custom post type, do not echo term-parent

I’ve made a custom function that retrieves the custom taxonomy term for a list of posts in a query, and echos both the name and the link. Additionally, it also excludes listing a term if I specify the term id (I have a special term, which I don’t want displayed, that I use as a loop hook).

This is achieved by this function

Read More
function my_get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_the_terms( $id, $taxonomy );

if ( is_wp_error( $terms ) )
    return $terms;

if ( empty( $terms ) )
    return false;

foreach ( $terms as $term ) {

    if(!in_array($term->term_id,$exclude)) {
        $link = get_term_link( $term, $taxonomy );
        if ( is_wp_error( $link ) )
            return $link;
        $term_links[] = '<a href="' . $link . '" rel="tag" class="category-link" title="See all stories from '.$terms_as_text.'">' . $term->name . '</a>';
    }
}

$term_links = apply_filters( "term_links-$taxonomy", $term_links );

return $before . join( $sep, $term_links ) . $after;
}

I then display it with this line

<?php echo my_get_the_term_list( $post->ID, 'sports_category',  '', ' ', '', array(3623) ); ?>

All well and good.

Now I am deeper into my site tree and I would like to retrieve the term/s of the current post once again, but now (as I am on the archive page for the term in above explanation) I only want to retrieve the sub-term/s and not display the parent term. I’ve tried modify the above, but I can’t seem to pull things apart.

The examples I’ve seen elsewhere don’t deal with custom terms, additionally I would like to form it into function so I can set some parameters as above.

This is my latest attempt

function my_get_the_children( $id, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_term_children( $id, $taxonomy );

if ( is_wp_error( $terms ) )
    return $terms;

if ( empty( $terms ) )
    return false;

foreach ( $terms as $term ) {

    if(!in_array($term->term_id,$exclude)) {
        $link = get_term_link( $term->name, $taxonomy );
        if ( is_wp_error( $link ) )
            return $link;
        $term_links[] = '<a href="' . $link . '" rel="tag" class="category-link" title="See all stories from '.$terms_as_text.'">' . $term->name . '</a>';
    }
}

$term_links = apply_filters( "term_links-$taxonomy", $term_links );

return $before . join( $sep, $term_links ) . $after;
}

Related posts

Leave a Reply

1 comment

  1. Here’s more of a complete guide based on the $wp_query object:

    The Taxonomy

    First you might want to know in which taxonomy you are, what its name is and retrieve all its available data from the object.

    // Taxonomy name
    $taxonomy  = get_query_var( 'taxonomy' );
    // Taxonomy object
    get_taxonomy( $taxonomy );
    // Taxonomy name
    get_taxonomy( $taxonomy )->label;
    

    The Taxon/Term

    Then you might want to do something with the current taxon/term.

    // The current taxon/term slug
    $term_slug = get_query_var( 'term' );
    // The complete current taxon/term object
    $term      = get_term_by( 'slug', $term_slug, $taxonomy );
    

    The Ancestors/Parents

    Getting the ancestors/parents offers a lot of possibilities. For example for a breadcrumb trail navigation or post meta data or simply to filter them out of the list of shown taxonomies.

    // Ancestors/Parents
    $ancestors = get_ancestors( 
         $term->term_id
        ,$taxonomy
    );
    foreach ( $ancestors as $ancestor )
    {
        // The currently looped parents/ancestors object
        $ancestor_obj = get_term( $ancestor, $taxonomy );
        // The currently looped parents/ancestors name
        $ancestor_name = $ancestor_obj->name;
    
        // Link to the parent/ancestor taxon/term archive
        $ancestor_link = get_term_link( $ancestor, $taxonomy )
    }
    

    Is it a hierarchical Taxonomy?

    You always will have to distinguish between hierarchical (category like) and flat (post tags like) taxonomies.

    // Are we in a hierarchical taxonomy?
    if ( is_taxonomy_hierarchical( $taxonomy ) )
    {
        // Do stuff
    }
    

    Do we have Children, my dear?

    Sometimes you’re in the middle of a really deeply nested hierarchical taxonomy. Then it makes sense to handle the children as well.

    // Based on the above retrieved data
    $children = get_term_children(
         $term->term_id
        ,$taxonomy
    );
    foreach ( $children as $child )
    {
        // The currently looped child object
        $child_obj = get_term( $child, $taxonomy );
        // The currently looped child name
        $child_name = $child_obj->name;
    
        // Link to the child taxon/term archive
        $child_link = get_term_link( $child, $taxonomy );
    }