我已经为“person”定义了一个自定义的post类型,我想根据与团队名称相对应的分类法将类名附加到输出标记中。例如,如果一个人是“创意”和“互动”团队的一部分,我想输出以下内容:
<div class="person creative interactive">Jane</div>
我发现this thread 在论坛上,但在我的例子中,我需要在echo
语句,因为输出在functions.php
文件这是我迄今为止提出的一个简化版本,基于上述线程和this Stack Overflow thread:
function display_all_people() {
// set up arguments for new post query
$args = array(
\'post_type\' => \'people\',
\'order\' => \'ASC\',
\'orderby\' => \'meta_value\',
\'meta_key\' => \'last_name\'
);
$peoplePosts = new WP_Query( $args );
if ( $peoplePosts->have_posts() ) {
echo \'<div class="people-listing">\';
while ( $peoplePosts->have_posts() ) {
$peoplePosts->the_post();
$terms = get_the_terms( $post->ID, \'teams\' );
foreach ($terms as $term) {
echo \'<div class="person\' . implode(\'\', $term->slug) . \'">\';
}
echo \'<div>More markup for each person listing</div>\';
echo \'</div>\';
}
echo \'</div>\';
} else {
return \'<p>Nothing Here.</p>\';
}
wp_reset_postdata();
}
因此,我试图使用implode()
连接数组的值(即“团队”分类名称),但它似乎不起作用(PHP正在抛出错误)你知道我如何以这种方式成功地将分类法附加为类名吗?感谢您的帮助。