2

I'm looking for a way to list all the terms from a custom taxonomy. Only the terms that have posts attached to it should have links to the archive page. If there are no posts attached it should only show the names.

Any ideas? Thanks!

<?php
$taxonomy = 'cat';
$queried_term = get_term_by( 'slug', get_query_var($taxonomy) );
$terms = get_terms($taxonomy);
if ( $terms !== 0 ) {
    foreach ( $terms as $term ) {
        echo $term->name . ", ";
    }
}
if ( $terms > 0 ) {
    foreach ( $terms as $term ) {
        echo '<li><a href="' . $term->slug . '">' . $term->name .'</a></li>';
    }
}
?>
2

I didn't understood your question well, but try this. Explanation is in the comments.

// your taxonomy name
$tax = 'post_tag';

// get the terms of taxonomy
$terms = get_terms( $tax, [
  'hide_empty' => false, // do not hide empty terms
]);

// loop through all terms
foreach( $terms as $term ) {

  // if no entries attached to the term
  if( 0 == $term->count )
    // display only the term name
    echo '<h4>' . $term->name . '</h4>';

  // if term has more than 0 entries
  elseif( $term->count > 0 )
    // display link to the term archive
    echo '<h4><a href="'. get_term_link( $term ) .'">'. $term->name .'</a></h4>';

}

Hope it helped you.

1

Thanks for your help! I've made some small adjustments and I got it working now:

<?php
// your taxonomy name
$tax = 'cat';

// get the terms of taxonomy
$terms = get_terms( $tax, $args = array(
  'hide_empty' => false, // do not hide empty terms
));

// loop through all terms
foreach( $terms as $term ) {

    // Get the term link
    $term_link = get_term_link( $term );

    if( $term->count > 0 )
        // display link to term archive
        echo '<a href="' . esc_url( $term_link ) . '">' . $term->name .'</a>';

    elseif( $term->count !== 0 )
        // display name
        echo '' . $term->name .'';
}
?>

This is great!

  • this is working great! Anyway I can get the current taxonomy I am at and highlight it? Thanks – gil hamer May 19 '16 at 7:23
0

Only an improvement to Schakelen's comment to test if something returned

// your taxonomy name
$tax = 'cat';

// get the terms of taxonomy
$terms = get_terms( $tax, $args = array( 
'hide_empty' => false, // do not hide empty terms
));

    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){                

        // loop through all terms
        foreach( $terms as $term ) {

            // Get the term link
            $term_link = get_term_link( $term );

            if( $term->count > 0 )
                // display link to term archive
                echo '<a href="' . esc_url( $term_link ) . '">' . $term->name .'</a>';

            elseif( $term->count !== 0 )
                // display name
                echo '' . $term->name .'';
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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