我假设您需要一个以逗号分隔的术语列表,其中包含链接。下面是PHP输出:
$post_regions = get_the_terms($post->ID,\'regions\');
//determines what the last region is so we know when to stop inserting commas between multiple regions
$region_keys = array_keys($post_regions);
$last_region_key = array_pop($region_keys);
echo(\'Region: \');
foreach ($post_regions as $key => $value) {
echo(\'<a href="\'.esc_url(get_tag_link($value->term_id)).\'" class="\'.$value->name.\'" >\'.$value->name.\'</a>\');
//only insert a comma between if it\'s not the last region
if ($key != $last_region_key){
echo(\', \');
}
}
当然,根据帖子上的实际条款,它应该以以下HTML结尾:
Region: <a href="#" rel="tag" class="USA">USA</a>,
<a href="#" rel="tag" class="EUR">EUR</a>,
<a href="#" rel="tag" class="JPN">JPN</a>,
<a href="#" rel="tag" class="Antarctica">Antarctica</a>
您可以使用CSS来设置样式。
Using Sass, you can save yourself the duplication of writing a declaration for each region using the each directive 像这样:
@each $flag in USA, EUR, JPN {
a.#{$flag} {
display:inline-block;
overflow:hidden;
width:0;
height:11px;
padding-left:16px;
background:url(\'REPLACE-WITH-YOUR-PATH-TO-IMAGES/#{$flag}.gif\');
}
}
Live demo on Codepen. 它将忽略您在SCS中未指定的任何区域名称,并显示这些区域名称的文本,但对于它识别的区域名称,它将隐藏文本并显示标志图像。如果不想处理Sass,可以在Codepen中更改CSS下的选项,以显示已编译的CSS并使用它。