0

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!

1 Answer 1

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>";
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.