Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

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', ' ', ' ', '' );
}
?>
share|improve this answer
    
I have tried it, it gives following error. HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request. –  Ahmad Mar 21 '12 at 18:07
1  
I fixed the PHP syntax in the answer, but, really: code snippets should never be treated as blind copy-and-paste material. Did you not check for syntax errors yourself, before pushing this code snippet live? –  Chip Bennett Mar 21 '12 at 18:13
    
I added ?> already but it's giving error. I am doing my same work with custom post type but now want to combine in post so need this code. Thanks –  Ahmad Mar 21 '12 at 18:18
    
Thank you sir. Quite fine. is there a way, if post is not related to that taxonomy then "All courses in" should also not appear? –  Ahmad Mar 21 '12 at 18:26
    
Thanks for the fixes @ChipBennett –  Tom J Nowell Mar 21 '12 at 20:40
show 2 more comments

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.

share|improve this answer
    
Thanks a lot @Maxoud . What if I want to include more terms along with 'country' or how to include php file with condition to if_taxonomy_exists. In that case, I can include as many terms required. –  Ahmad Mar 24 '12 at 6:51
    
@Ahmad: I've edited the code above. Not tested though. –  Max Yudin Mar 26 '12 at 12:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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