So I have a code where all items of my custom taxonomy ('colour') are displayed. I broke my head trying to show the section ("colour__chart") only if one of taxonomy items was chosen. In simple words, how can I show a section if at least one taxonomy item (tag for example) is chosen, and not to show this section if taxonomy item is not chosen?

<section class="colour__chart">
    <div class="container">
        <div id="color-chart">
               <h2>Colour Chart</h2>
               <hr class="divider">
                <?
            $terms = get_the_terms( get_the_ID(), 'colour' );
if( $terms && ! is_wp_error( $terms ) ){
    foreach( $terms as $term ){

        echo get_field( 'name', 'colour_' . $term->term_id ); 

        echo '<div class="col-md-2 col-sm-4 col-xs-4 text-center">';
        echo '<div class="color-chart__item" style="background-color:' . get_field( 'colour_acf', 'colour_' . $term->term_id ) . '"></div>';
        echo '<p>' . $term->name . '</p>' ;
        echo '</div>' ;
    }
}
            ?>
            </div>
    </div>
</section>

I tried something like this but it doesn't work unfortunately

<?php if ( is_tax('colour',) ) { ?>
    <section class="colour__chart">
        <div class="container">
            <div id="color-chart">
                   <h2>Colour Chart</h2>
                   <hr class="divider">
                    <?
                $terms = get_the_terms( get_the_ID(), 'colour' );
    if( $terms && ! is_wp_error( $terms ) ){
        foreach( $terms as $term ){

            echo get_field( 'name', 'colour_' . $term->term_id ); 

            echo '<div class="col-md-2 col-sm-4 col-xs-4 text-center">';
            echo '<div class="color-chart__item" style="background-color:' . get_field( 'colour_acf', 'colour_' . $term->term_id ) . '"></div>';
            echo '<p>' . $term->name . '</p>' ;
            echo '</div>' ;
        }
    }
                ?>
                </div>
        </div>
    </section>
    }
    <?php } ?>
  • OK, let me make that clear. You want to show this chart only if given post has one of colors assigned? Right? – Krzysiek Dróżdż Dec 2 at 11:50
  • Hi @KrzysiekDróżdż yes you are correct – Jamdev Dec 2 at 11:59
up vote 1 down vote accepted

You already check, if given post has any terms assigned. All you need to do is to move that check earlier in the code.

<?php 
    $terms = get_the_terms( get_the_ID(), 'colour' );
    if ( $terms && ! is_wp_error( $terms ) ) :
?>
<section class="colour__chart">
    <div class="container">
        <div id="color-chart">
            <h2>Colour Chart</h2>
            <hr class="divider">
            <?php
                foreach ( $terms as $term ) {
                    echo get_field( 'name', 'colour_' . $term->term_id ); 

                    echo '<div class="col-md-2 col-sm-4 col-xs-4 text-center">';
                    echo '<div class="color-chart__item" style="background-color:' . get_field( 'colour_acf', 'colour_' . $term->term_id ) . '"></div>';
                    echo '<p>' . $term->name . '</p>' ;
                    echo '</div>' ;
                }
            ?>
        </div>
    </div>
</section>
<?php endif; ?>
  • Works perfectly! Thank you so much! – Jamdev Dec 2 at 12:18

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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