Related Docs

Did not find what you're looking for? Search the complete TukuToi Software Documentation

ShortCode

Shortcode to return the parent term ID of a given term

Sometimes you want to get the ID (or other data) of the parent of a given term in a hierarchical taxonomy.

With below shortcode you can do just that. It takes a term ID of which to get the parent and a taxonomy to which the term belongs to.

// ShortCode to get current term parent ID. 
add_shortcode( 'term_parent_id', 'term_parent_id_fn' ); 
/**  
 * ShortCode to get current term parent ID..  
 *   
 * @param array $atts ShortCode attributes.
 */
function term_parent_id_fn( $atts ){ 
    $atts = shortcode_atts(  array( 
            'term'  => 0, 
            'tax'  => '',
        ),  
        $atts,  
        'term_parent_id'  
    ); 
    $term  = get_term( $atts['term'], $atts['tax'] ); 
    $parent = $term->parent == 0 ? 0 : get_term( $term->parent ); 
    $parent_id = ''; 
    if( 0 !== $parent ){ 
        $parent_id = $parent->term_id; 
    } 
    return $parent_id; 
}