get_terms only show term if there is a post using it
get_terms should hide empty terms by default, but you can force it to setting hide_empty argument to true: $terms = get_terms( array( ‘taxonomy’ => ‘image_categories’, ‘hide_empty’ => false, ) );
get_terms should hide empty terms by default, but you can force it to setting hide_empty argument to true: $terms = get_terms( array( ‘taxonomy’ => ‘image_categories’, ‘hide_empty’ => false, ) );
If you’re on a single post, then your code won’t work, because get_queried_object will contain the post (and not a term). If you want to get terms of given post, you should use get_the_terms function. $terms = get_the_terms( get_the_ID(), ‘<TAXONOMY_NAME>’ ); // <- put real tax name in here You also have to remember, that … Read more
if ( is_wp_error($term_id) ) { // oops WP_Error obj returned, so the term existed prior // echo $term_id->get_error_message(); } See if that works for you.
I think you should use a custom loop: <?php $terms = get_the_terms( $post->ID, ‘product_tag’ ); if ($terms && ! is_wp_error($terms)): ?> <?php foreach($terms as $term): ?> <a href=”https://wordpress.stackexchange.com/questions/130622/<?php echo get_term_link( $term->slug,”product_tag’); ?>” rel=”tag” class=”<?php echo $term->slug; ?>”><?php echo $term->name; ?></a> <?php endforeach; ?> <?php endif; ?>
Hi @NetConstructor: First thing, assuming your logic worked you can use the ternary operator to simplify your example: <li id=”kids-<?php echo is_term(‘Kids’,’age_groups’) ? ‘on’ : ‘off’; ?>”>Kids Programs</li> The issue seems to be that is_term() is used to check if a term exists, not if it is associated with a particular post. I think what … Read more
After WordPress has updated a posts’ taxonomy terms it triggers the action: set_object_terms (see source) which passes 6 variables: Object ID Terms that were assigned to the post (or added depending on 5) (could be their slug or term ID). Taxonomy-term IDs of above (not term IDs) The taxonomy Append. If true – terms in … Read more
OK had a go at this myself, I didn’t realise ACF was able to add fields to taxonomies which is really handy so I wanted to figure it out too. <?php $libargs=array( ‘hide_empty’ => 0, ‘parent’ => 0, ‘taxonomy’ => ‘library_categories’); $libcats=get_categories($libargs); foreach($libcats as $lc){ $termlink = get_term_link( $lc->slug, ‘library_categories’ ); ?> <a class=”single-library-cat” href=”https://wordpress.stackexchange.com/questions/71287/<?php … Read more
What about doing a tax_query? $args = array( ‘post_type’ => ‘myposttype’, ‘tax_query’=> array( ‘taxonomy’ => ‘myposttype_categories’, ‘terms’ => array(‘foo’), ‘field’ => ‘slug’, ) ); $loop = new WP_Query( $args ); var_dump($loop); Facepalm question, you are sure that these taxonomies/post types exist and that there are posts filed under them? Update The query seems to work … Read more
Simply omit (or not add) tax_query part of arguments. $args = array( ‘post_type’ => ‘wr_event’, ‘posts_per_page’ => -1, ‘meta_key’ => ‘event_date’, ‘orderby’ => ‘meta_value_num’, ‘order’ => $order, ‘meta_value’ => $yesterday, ‘meta_compare’ => $compare, ); if ( ! is_null($cat) ) $args[‘tax_query’] =array( array( ‘taxonomy’ => ‘event_type’, ‘field’ => ‘slug’, ‘terms’ => $cat, ‘operator’ => ‘IN’ ), … Read more
You can make use of the hide_empty argument of get_terms(). It’s default value is set to true. Do it somewhat like this: $args = array( ‘hide_empty’ => false ); $terms = get_terms( ‘wpsc_product_category’, $args );