samedi 28 novembre 2015

foreach and if statement to display custom taxonomies

I am trying to display custom taxonomies but if there are none to display a message.

Here is as far as I have gotten:

<?php
    $terms = get_the_terms( $post->ID , 'machine-features' );
    foreach ( $terms as $term ) {
        if ($term) {
            echo '<li>' . $term->name . '</li>';
        } else {
            echo "Contact us for details";
        }
    }
?>  

It works perfectly when there are features added but when empty I get the following error: Warning: Invalid argument supplied for foreach()

I'm new to php so apologies if this is a crazy basic question - I searched for an answer and tried various permutations but nothing worked!

Thanks a million for any help! O

Aucun commentaire:

Enregistrer un commentaire