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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

hide_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