Exclude Custom Taxonomy Terms from Genesis [post_info] shortcode in WordPress
I needed to exclude specific custom taxonomy terms from the Genesis [post_info] shortcode which by default lists all the terms in the taxonomy in the post meta.
I achieved this by modifying the get_the_terms_list() function, which is used in the Genesis post_info shortcode. Then create a new shortcode [filtered_post_info] to use instead of [post_info].
The first step is creating a new function based on modifying the core WordPress function: get_the_term_list to add an attribute for ‘exclude’.
function get_modified_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
if(!in_array($term->term_id,$exclude)) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
}
}
if( !isset( $term_links ) )
return false;
return $before . join( $sep, $term_links ) . $after;
}
Then we modify the Genesis default post_terms shortcode and call our new get_modified_terms_list() function and feed it the IDs we want to exclude.
(look at line 480 in Genesis > post.php for the original code)
//New shortcode to replace [post_terms]
add_shortcode( 'filtered_post_terms', 'filtered_post_terms_shortcode' );
function filtered_post_terms_shortcode( $atts ) {
$defaults = array(
'after' => '',
'before' => __( 'Filed Under: ', 'genesis' ),
'sep' => ', ',
'taxonomy' => 'category',
);
$defaults = apply_filters( 'genesis_post_terms_shortcode_defaults', $defaults );
$atts = shortcode_atts( $defaults, $atts, 'post_terms' );
//This is the only part of the original Genesis shortcode that needs to be modified
//$terms = get_the_term_list( get_the_ID(), $atts['taxonomy'], $atts['before'], trim( $atts['sep'] ) . ' ', $atts['after'] );// This is the old way (Genesis original)
//Use the modified term list and add the IDs of terms to exclude
$terms = get_modified_term_list( get_the_ID(), $atts['taxonomy'], $atts['before'], trim( $atts['sep'] ) . ' ', $atts['after'], array(26,108,112,129) );
if ( is_wp_error( $terms ) )
return;
if ( empty( $terms ) )
return;
if ( genesis_html5() )
$output = sprintf( '<span %s>', genesis_attr( 'entry-terms' ) ) . $terms . '</span>';
else
$output = '<span class="terms">' . $terms . '</span>';
return apply_filters( 'genesis_post_terms_shortcode', $output, $terms, $atts );
}
Now we have a new shortcode: [filtered_post_terms] which can be used to replace the original [post_terms] shortcode, it accepts the same attributes:
For example: [filtered_post_terms taxonomy=”my custom taxonomy” before=”Custom Taxonomy Topics: “]
This is so perfect, I’ve been looking for this for weeks. You just saved my launch date. THANK YOU!
Is there a way to edit this so that it hides all children instead of adding each ID manually?
Hi Christina,
It wouldn’t be difficult to create a small function that gets the children of your terms, merges them together and feeds that to the exclusion list.
Have a look at: get_term_children()
and
array_merge()