0

I have this code, below and I want to change it to hide taxonomy when term is empty:

                <ul class="info-list">
                    <li>
                        <label><?php echo esc_html__( 'Actor:', 'amy-movie' ); ?></label>
                        <span><?php echo amy_movie_get_movie_taxomony( $post->ID, 'amy_actor' ); ?></span>
                    </li>
                    <li>
                        <label><?php echo esc_html__( 'Director:', 'amy-movie' ); ?></label>
                        <span><?php echo amy_movie_get_movie_taxomony( $post->ID, 'amy_director' ); ?></span>
                    </li>
                        <label><?php echo esc_html__( 'Genre:', 'amy-movie' ); ?></label>
                        <span><?php echo amy_movie_get_movie_taxomony( $post->ID, 'amy_genre' ); ?></span>
                    </li>
                    <li>
                        <label><?php echo esc_html__( 'Release:', 'amy-movie' ); ?></label>
                        <span><?php echo esc_attr( date( get_option( 'date_format' ), strtotime( $mdetails['movie_release'] ) ) ); ?></span>
                    </li>
                    <li>
                        <label><?php echo esc_html__( 'Language:', 'amy-movie' ); ?></label>
                        <span><?php echo esc_attr( $mdetails['movie_language'] ); ?></span>
                    </li>
                    <?php if ( amy_get_option( 'enable_m_cinema', true ) == true ) : ?>
                    <li>
                        <label><?php echo esc_html__( 'Cinema:', 'amy-movie' ); ?></label>
                        <?php
                        if ( ! empty( $cinemas ) ) {
                            $numItems   = count( $cinemas );
                            $i          = 0;

                            foreach ( $cinemas as $cinema ) {
                                if ( ++$i === $numItems ) {
                                    $space = '';
                                } else {
                                    $space = ', ';
                                }

                                ?>
                                <a href="<?php echo get_permalink( $cinema ); ?>"><?php echo get_the_title( $cinema ); ?></a>
                                <?php echo esc_attr( $space ); ?>
                            <?php
                            }
                        }
                        ?>
                    </li>
                    <?php endif; ?>
                </ul> 
1
  • add an if wrapping the echo of the taxonomy
    – David Lee
    Sep 18 '17 at 18:00
1

Lots of Automattic themes do exactly this for standard tags and categories The Underscores starter theme, too. Straight from its source code up on GitHub:

$categories_list = get_the_category_list( esc_html__( ', ', '_s' ) );
if ( $categories_list ) {
    /* translators: 1: list of categories. */
    printf( '' . esc_html__( 'Posted in %1$s', '_s' ) . '', $categories_list ); // WPCS: XSS OK.
}

Adapt as needed for your taxonomy.

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.