Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Conditional IF /ELSE statements with taxonomy WordPress

  • SOLVED

I've been trying to get a conditional if/else statement within a single post to work. I have a taxonomy of 'artist' and I would like posts with the value of 'various-artists' to display differently than other values. Here is the code I've working with:

<?php if ( artist( 'various-artists' ) ) {
echo '<h2>Various Artists</h2>';
} else {
echo '<h2><?php echo get_the_term_list( $post->ID, 'artist' ); ?></h2>'; } ?>


Thank you for your answers and please let me know if you need any other info.

Answers (3)

2011-04-11

Ivaylo Draganov answers:

Hi,

there's an undocumented function for checking whether a post has a specific term. Here's how I've tweaked your code:

<?php

if ( has_term( 'various-artists', 'artist', $post->ID ) ) {

echo '<h2>Various Artists</h2>';

} else {

echo '<h2>' . get_the_term_list( $post->ID, 'artist' ) . '</h2>';

}

?>


Use that in the loop. Let me know how it went :)


Jeremy Phillips comments:

That did the job beautifully! Thanks a lot!

2011-04-11

Michael Fields answers:

Something like this should work inside the loop:


<?php
$terms = get_the_terms( get_the_ID(), 'artist' );
if ( is_array( $terms ) && in_array( 'various-artists', $terms ) ) {
print '<h2>Various Artists</h2>';
}
else {
print '<h2>' . get_the_term_list( get_the_ID(), 'artist' ) . '</h2>';
}
?>

2011-04-11

Maor Barazany answers:


<?php
global $post;
$post_terms = get_the_terms($post->ID, 'artist'); //get array of artists taxonomy objects
//now we will make array with terms from this taxonomy of this post
foreach($post_terms as $t) {
$termname[] = $t->name;
}
//now we can check the term we want with that array we created
if (in_array('various-artists',$termname) {
echo '<h2>Various Artists</h2>';

} else {

echo '<h2><?php echo get_the_term_list( $post->ID, 'artist' ); ?></h2>'; } ?>