Php Wordpress获取每个帖子的特定类别的术语并显示出来
我想做的是获取每个帖子的特定类别的术语,然后在这个帖子中显示它 我所写的代码应该很简单:Php Wordpress获取每个帖子的特定类别的术语并显示出来,php,wordpress,Php,Wordpress,我想做的是获取每个帖子的特定类别的术语,然后在这个帖子中显示它 我所写的代码应该很简单: function wodarchive_entry($post_id = null) { $post = get_post( $post_id ); $termsArray = get_the_terms( $post->ID, "category" ); //Get the terms for this particular item $termsString = ""; //in
function wodarchive_entry($post_id = null) {
$post = get_post( $post_id );
$termsArray = get_the_terms( $post->ID, "category" );
//Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->name.' ';
$category_id = $term->parent;//create a string that has all the slugs
if($category_id == '203'){
echo $termsString;
}
}
}
顺便说一句,这不起作用。嗯,差不多了。它还为我提供了其他类别的值,可能是因为if语句在找到条件之前一直在播放。如果您只想在
$termsString
中获取匹配项,则应将赋值放在if循环中
function wodarchive_entry($post_id = null) {
$termsArray = get_the_terms( $post_id, "category" );
//Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$category_id = $term->parent;//create a string that has all the slugs
if($category_id == '203'){
$termsString .= $term->name.' ';
}
}
echo $termsString;
}
你想显示这203个分类id帖子吗?我真是太蠢了。非常感谢,我看不到。谢谢顺便问一下,你认为在wordpress中有更好的方法实现同样的目标吗?很高兴我能提供帮助。您可以创建一个自定义分类法来添加术语,这样您就不需要使用if循环。