• Resolved WebStyley

    (@webstyley)


    Hello, I am using the below code to display taxonomies on a page template. The taxonomies are displaying, but they are not hiding the empty terms (no content associated with the taxonomy). Any ideas on how to hide empty terms with the below code, or even a different code? Thanks

    <?php
    
    $args = array( 'hide_empty=1', 'hierarchical=1' );
    
    $terms = get_terms( 'service-area', $args );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        $count = count( $terms );
        $i = 0;
        $term_list = '<p class="my_term-archive">';
        foreach ( $terms as $term ) {
            $i++;
            $term_list .= '<a href="' . esc_url( get_term_link( $term ) ) . '" alt="' . esc_attr( sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) ) . '">' . $term->name . '</a>';
            if ( $count != $i ) {
                $term_list .= ', ';
            }
            else {
                $term_list .= '</p>';
            }
        }
        echo $term_list;
    }
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hey WebStyley,

    I think your issue is a typo on the first line of your code snippet. Try $args = array( 'hide_empty' = 1, 'hierarchical' = 1 );

    Hope that resolves it for you!
    –AJ

    Thread Starter WebStyley

    (@webstyley)

    AJ thanks for the reply. That change gives me a 500 error? Any other ideas on how to hide empty taxonomies.

    WebStyley, yes try this:

    $args = array( 'hide_empty' => 1, 'hierarchical' => 1 );

    Sorry I didn’t get the syntax correct the first time though.

    –AJ

    Thread Starter WebStyley

    (@webstyley)

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Hide Empty Taxonomy’ is closed to new replies.