0

I'm using this function to get the categories linked to the post:

the_terms($post->ID, 'portfolio-type', '<p><b>'.__('Categories:','om_theme').'</b> ', ', ', '</p>');

the result is this:

Categories: 16 Max, Capacity

The category Capacity is parent of 16 Max category. I'm looking to exclude parents so the result would be:

Categories: 16 Max

I appreciate any help!

1 Answer 1

0

use get_the_terms instead and exclude any terms where parent value is 0, which means it is a top-level term.

$terms = get_the_terms( $post->ID, 'portfolio-type' );
if ( !empty( $terms ) ) {
    $output = array();
    foreach ( $terms as $term ){
        if( 0 != $term->parent )
            $output[] = '<a href="' . get_term_link( $term ) .'">' . $term->name . '</a>';
    }
    if( count( $output ) )
        echo '<p><b>' . __('Categories:','om_theme') . '</b> ' . join( ", ", $output ) . '</p>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.