admin管理员组文章数量:1279118
I'm using the following to populate terms on my pages...
$mget_site_url = get_site_url();
//This gets me all the correct terms...
$mget_the_terms = get_the_terms(get_queried_object_id(), 'ht_kb_tag');
echo __( 'Tagged: ', 'knowall' );
foreach ($mget_the_terms as $keygroup => $valuegroup) {
foreach ($valuegroup as $key => $value) {
if (($key == 'name') && ($value != 'layout-wide')) {
//link the terms
echo "<a href='{$mget_site_url}/tags/{$value}' rel='tag'>{$value}</a>";
}
}
}
How can I conditionally cancel this process if the page does not have any terms? Trying to avoid the foreach error I'm seeing.
Many thanks!
I'm using the following to populate terms on my pages...
$mget_site_url = get_site_url();
//This gets me all the correct terms...
$mget_the_terms = get_the_terms(get_queried_object_id(), 'ht_kb_tag');
echo __( 'Tagged: ', 'knowall' );
foreach ($mget_the_terms as $keygroup => $valuegroup) {
foreach ($valuegroup as $key => $value) {
if (($key == 'name') && ($value != 'layout-wide')) {
//link the terms
echo "<a href='{$mget_site_url}/tags/{$value}' rel='tag'>{$value}</a>";
}
}
}
How can I conditionally cancel this process if the page does not have any terms? Trying to avoid the foreach error I'm seeing.
Many thanks!
Share Improve this question asked Nov 19, 2021 at 19:11 klewisklewis 8991 gold badge14 silver badges29 bronze badges1 Answer
Reset to default 0hide_empty
hides terms that have no posts attached to them. You've got a page with no ht_kb_tags
attached to it, if I'm understanding the question correctly.
Since get_the_terms()
returns false
if there are no terms, you should be able to prevent the foreach()
with an if()
statement:
$mget_site_url = get_site_url();
//This gets me all the correct terms...
$mget_the_terms = get_the_terms(get_queried_object_id(), 'ht_kb_tag');
if ( ! empty( $mget_the_terms ) ) {
echo __( 'Tagged: ', 'knowall' );
foreach ($mget_the_terms as $keygroup => $valuegroup) {
foreach ($valuegroup as $key => $value) {
if (($key == 'name') && ($value != 'layout-wide')) {
//link the terms
echo "<a href='{$mget_site_url}/tags/{$value}' rel='tag'>{$value}</a>";
}
}
}
}
本文标签: termsCan we apply hide empty to gettheterms
版权声明:本文标题:terms - Can we apply hide empty to get_the_terms? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741213521a2359599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论