简体   繁体   中英

Output taxonomy terms - show none if none selected

I've got a working shortcode that outputs the terms of a taxonomy as image thumbnails.

It works great, BUT when none of the taxonomy terms are selected, it outputs ALL terms.

My current code is as follows:

add_shortcode( 'book-accreditation', 'book_accreditation_output' );
function book_accreditation_output($atts){

    ob_start();
    echo '<div id="book-terms-wrap"><ul class="book-terms">';

    global $post;
    $taxonomy = 'book_accreditation';
    $post_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids'));

    $terms = get_terms( $taxonomy, array(
    'hide_empty' => false,
    'include'  => $post_terms
      )
  );

  foreach($terms as $term){
    $thumbnail = get_field('bkk_taxonomy_image', $term->taxonomy . '_' . $term->term_id);
    $thumbnail = $thumbnail['url'];
    $name = $term->name;
    $url = get_term_link($term, $taxonomy);
    echo '<li><img src="' . $thumbnail . '" class="accreditation-image"></li>';
  }
    echo '</ul></div>';
    $output = ob_get_clean();
    return $output;
}

I'm trying to fix this with if (!empty()) variable targeting the $terms , but i'm probably not putting the code correctly.

So if NO terms are selected, then nothing should output.

I'm adding the code as follows:

add_shortcode( 'book-accreditation', 'book_accreditation_output' );
function book_accreditation_output($atts){

  global $post;
  $taxonomy = 'book_accreditation';
  $post_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids'));

  $terms = get_terms( $taxonomy, array(
  'hide_empty' => false,
  'include'  => $post_terms
  )
  );

  if (!empty($terms)) {
    ob_start();
    echo '<div id="book-terms-wrap"><ul class="book-terms">';

    foreach($terms as $term){
    $thumbnail = get_field('bkk_taxonomy_image', $term->taxonomy . '_' . $term->term_id);
    $thumbnail = $thumbnail['url'];
    $name = $term->name;
    $url = get_term_link($term, $taxonomy);
    echo '<li><img src="' . $thumbnail . '" class="accreditation-image"></li>';
    }
    echo '</ul></div>';
    $output = ob_get_clean();
    return $output;
  }
}

But this won't work unfortunately, what am I doing wrong here?

You have the right idea but are checking in the wrong spot.

wp_get_object_terms returns an empty array if the Post has no such terms.

get_terms, namely the WP_Term_query array of arguments passed in , accepts the includes parameter as you have it, but if you don't provide includes the default includes is an empty array. That is, passing in an empty array is identical to not passing the parameter at all - meaning, it ignores the includes limitation entirely and returns all results matching any other limits (in your case, it simply returns all of them).

So in brief, you know that you have no terms as soon as your $post_terms line is called. You can use the following:

$post_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids'));

// No terms found, skip the rest of the function
if ( empty($post_terms) )
  return false

$terms = get_terms( $taxonomy, array(
  'hide_empty' => false,
  'include'  => $post_terms
)

As an aside, I'm reasonably certain you can also skip the get_terms call entirely by changing the wp_get_object_terms line as follows:

$terms = wp_get_object_terms($post->ID, $taxonomy, array('hide_empty' => false) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM