If taxonomy exists then to show some code

I want to insert following custom code in single.php
can I set condition that if specific taxonomy exist then show and work this code otherwise not;

<div id="archivebox"> All courses in<?php echo get_the_term_list($post->ID,'country', ' ', ' ', '' ) ; ?><br>All courses in<?php echo get_the_term_list($post->ID,'institute', ' ', ' ', '' ) ; ?><br>All <?php echo get_the_term_list($post->ID,'subject', ' ', ' ', '', '' ) ; ?> courses worldwide<br>All<?php echo get_the_term_list($post->ID,'qualification', ' ', ' ', '' ) ; ?> courses worldwide<br>Alphabetical List: <?php echo get_the_term_list($post->ID,'alphabetical', ' ', ' ', '' ) ; ?> worldwide</div>

Related posts

Leave a Reply

2 comments

  1. If you want to check for the existence of a taxonomy, use taxonomy_exists( $taxonomy ):

    <?php
    if(taxonomy_exists('country')){
         echo 'All courses in' . get_the_term_list($post->ID,'country', ' ', ' ', '' );
    }
    ?>
    

    etc…

    Edit

    If, instead of checking for the existence of a taxonomy, you want to check if the current post belongs to a taxonomy, use get_the_term_list( $post->ID, $taxonomy ):

    <?php
    if( false != get_the_term_list( $post->ID, 'country' ) ) {
         echo 'All courses in' . get_the_term_list($post->ID,'country', ' ', ' ', '' );
    }
    ?>
    
  2. To avoid duplicate post I’ll answer here:

    <?php
    $my_terms = array('country', 'region', 'city');
    $terms_obj =  wp_get_object_terms($post->ID, $my_terms);
    if( !empty($terms_obj) ) {
        if( !is_wp_error( $terms_obj ) ) {
            echo '<div id="archivebox">All courses in ';
            foreach($terms_obj as $term) {
                foreach($my_terms as $my_term) {
                    echo '<a href="'.get_term_link($term->slug, $my_term).'">'.$term->name.'</a> ';
                }
            }
            echo '</div>';
        }
    }
    ?>
    

    Edited according comments below.