Wordpress,回显带有自定义 link 的 slug 术语列表
Wordpress, echo list of slug terms with custom link
我正在尝试使用我的自定义 link 获取术语列表(使用 CPT UI),其中 slug 是自定义 link 的最后一部分。
示例:
客户:Term1、Term2、Term3
其中每个术语都是 link,例如:
example.com/#term1
example.com/#term2
example.com/#term3
所以,我有相同的自定义 link 结构,但只有最后一个 slug 发生了变化:
$servizio = get_the_terms($post->ID, 'servizio');
$servizio = array_values($servizio);
for($cat_count=0; $cat_count<count($servizio); $cat_count++) {
echo $servizio[$cat_count]-> slug;
if ($cat_count<count($servizio)-1){
echo ', ';
}
}
改用foreach
循环,这样更容易迭代。
$categories = get_the_terms(get_the_ID(), 'servizio');
$categories_output = [];
if ($categories) { // Prevent the error #Invalid argument supplied for foreach()# incase the post has no category
foreach ($categories as $category) {
$categories_output[] = '<a href='. get_bloginfo('url') . '/#' . $category->slug .'>'. $category->name .'</a>';
}
}
if ($categories_output) {
echo implode(', ', $categories_output);
}
我正在尝试使用我的自定义 link 获取术语列表(使用 CPT UI),其中 slug 是自定义 link 的最后一部分。
示例: 客户:Term1、Term2、Term3
其中每个术语都是 link,例如:
example.com/#term1
example.com/#term2
example.com/#term3
所以,我有相同的自定义 link 结构,但只有最后一个 slug 发生了变化:
$servizio = get_the_terms($post->ID, 'servizio');
$servizio = array_values($servizio);
for($cat_count=0; $cat_count<count($servizio); $cat_count++) {
echo $servizio[$cat_count]-> slug;
if ($cat_count<count($servizio)-1){
echo ', ';
}
}
改用foreach
循环,这样更容易迭代。
$categories = get_the_terms(get_the_ID(), 'servizio');
$categories_output = [];
if ($categories) { // Prevent the error #Invalid argument supplied for foreach()# incase the post has no category
foreach ($categories as $category) {
$categories_output[] = '<a href='. get_bloginfo('url') . '/#' . $category->slug .'>'. $category->name .'</a>';
}
}
if ($categories_output) {
echo implode(', ', $categories_output);
}