admin管理员组文章数量:1316691
I have created a function to display my custom taxonomy ('market') terms. But the problem is that this outputs the first taxonomy term twice. Here is my function:
function related_markets()
{
if ('news' === get_post_type()) {
$terms = get_terms(array(
'taxonomy' => 'market',
'hide_empty' => false,
));
foreach ($terms as $term) {
$term_list .= '<a class="related-market btn btn-outline-secondary" href="' . esc_url(get_term_link($term)) . '">' . $term->name . '</a>';
echo $term_list;
}
} elseif ('analysis' === get_post_type()) {
$terms = get_terms(array(
'taxonomy' => 'market',
'hide_empty' => false,
));
foreach ($terms as $term) {
$term_list .= '<a class="related-market btn btn-outline-secondary" href="' . esc_url(get_term_link($term)) . '">' . $term->name . '</a>';
echo $term_list;
}
}
}
endif;
I have created a function to display my custom taxonomy ('market') terms. But the problem is that this outputs the first taxonomy term twice. Here is my function:
function related_markets()
{
if ('news' === get_post_type()) {
$terms = get_terms(array(
'taxonomy' => 'market',
'hide_empty' => false,
));
foreach ($terms as $term) {
$term_list .= '<a class="related-market btn btn-outline-secondary" href="' . esc_url(get_term_link($term)) . '">' . $term->name . '</a>';
echo $term_list;
}
} elseif ('analysis' === get_post_type()) {
$terms = get_terms(array(
'taxonomy' => 'market',
'hide_empty' => false,
));
foreach ($terms as $term) {
$term_list .= '<a class="related-market btn btn-outline-secondary" href="' . esc_url(get_term_link($term)) . '">' . $term->name . '</a>';
echo $term_list;
}
}
}
endif;
Share
Improve this question
edited Nov 10, 2020 at 12:17
Farzad
asked Nov 10, 2020 at 12:12
FarzadFarzad
33 bronze badges
1 Answer
Reset to default 3I think you want to show list of the category links right? but you are echo list inside the for-each loop. you have to write that outside of the loop.
foreach ($terms as $term) {
$term_list .= '<a class="related-market btn btn-outline-secondary" href="' . esc_url(get_term_link($term)) . '">' . $term->name . '</a>';
}
echo $term_list;
Also don't forgot to initialize "$term_list" first in the function. :)
本文标签: getterms() duplicate first term of a custom taxonomy
版权声明:本文标题:get_terms() duplicate first term of a custom taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742005259a2411826.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论