问题
I'm struggling to get this to work.
Basically, I'm running a WP query to retrieve multiple posts from my custom post-type.
In the loop, I want to conditionally echo stuff, depending on what term is assigned to that post.
I've created a taxonomy called 'file-types' and have assigned this to my custom post type.
As I understand terms, terms are like the children of taxonomy. "Not literally"
My problem, my PHP below just seems to echo the last condition, which is nothing.
And I've 100% assigned these terms to my posts.
The only thing I have not tried is changing 'term_taxonomy' to a 'term_taxonomy_id' - but I cannot for the life of me find out how to find the ID of my taxonomy.
Can any one help me understand why nothing is being echo'ed? Many Thanks
<?php
if (term_exists(array(
'term_id' => 4,
'term_taxonomy' => 'file-formats'
))) {
echo 'PDF' ;
}
else if (term_exists(array(
'term_id' => 6,
'term_taxonomy' => 'file-formats'
))) {
echo 'PPT' ;
}
else if (term_exists(array(
'term_id' => 5,
'term_taxonomy' => 'file-formats'
))) {
echo 'MOV' ;
}
else {
echo '' ;
}
?>
回答1:
The problem is that term_exists() doesn't except arrays like you have it.
See here for details on how to use it: http://codex.wordpress.org/Function_Reference/term_exists
This is how it needs to be:
if( term_exists(5, 'file-formats') ) {
} elseif( term_exists(6, 'file-formats') ) {
} else {
}
I think you can also feed in a slug or name too:
term_exists('PDF', 'file-formats')
来源:https://stackoverflow.com/questions/9617533/wordpress-term-exist-not-echoing-anything