首先,你可以foreach
在…内foreach
. 事实上,您可以根据服务器配置将循环嵌套到更高的级别。
如果要在该字段中仅使用一个术语,请确保选择Field Type 像Radio 或Select 和Return Value 像Term Object. 在这种情况下,您的代码应该如下所示:
<?php
if( get_terms(\'shows\') ) {
foreach( get_terms(\'shows\') as $term ) {
$topic_term = get_field(\'podcast_topic\', $term); // This should be a Term Object
// Optional: you can use $topic_term to get ACF data from that selected term
$topic_custom_thumb = get_field(\'podcast_topic_thumb\', $topic_term);
echo \'<div class="item \' . esc_attr( $topic_term->slug ) . \' col-4 col-md-3 col-lg-2">\';
echo \'<a href="\' . get_term_link( $term ) . \'">\'; // You were making the term link in wrong way
echo wp_get_attachment_image( get_field(\'podcast_category_thumb\', $term), \'thumbnail\', false);
echo \'</a></div>\';
}
}
?>
如果您希望选择多个术语作为播客主题字段,则
Field Type 应该是
Multi Select 或
Checkbox. 在这种情况下,使用
get_field()
. 那么您的代码应该是这样的:
<?php
if( get_terms(\'shows\') ) {
foreach( get_terms(\'shows\') as $term ) {
$topic_terms = get_field(\'podcast_topic\', $term); // This should be an array of Term Objects
$classes = []; // Let\'s store slugs of each term here
if( is_array($topic_terms) && !empty($topic_terms) ) {
foreach( $topic_terms as $topic_term ) {
$classes[] = $topic_term->slug;
}
}
echo \'<div class="item \' . esc_attr( implode( \' \', $classes ) ) . \' col-4 col-md-3 col-lg-2">\';
echo \'<a href="\' . get_term_link( $term ) . \'">\'; // You were making the term link in wrong way
echo wp_get_attachment_image( get_field(\'podcast_category_thumb\', $term), \'thumbnail\', false);
echo \'</a></div>\';
}
}
?>
记住,当你
Term Object 从…起
get_field()
, 您还可以使用该对象从该术语中获取ACF数据。第一个代码块中的示例。