Genesis Tip — How To Display Custom Taxonomy Terms In Post Meta

CodeGenesis offers post meta information in the entry footer; which by default includes all the categories and tags (default WordPress taxonomies) assigned to the post. However, this does not hold true for custom taxonomies.

For one of my custom Genesis themes I wanted to include custom taxonomies in place of regular WordPress categories and tags. But there is no readily available WordPress / Genesis plugin or shortcode to pull up this information. So, I created the following shortcode to display custom taxonomy terms.

Fire-up your functions.php and add the following code:

/** A custom shortcode to fetch terms of a custom taxomomy for any post **/
add_shortcode( 'bt-custom-post-meta', 'bt_custom_post_meta' );
function bt_custom_post_meta( $atts ) {
	$defaults = array(
		'prepend' => 'Listed Under: ', // Text to be added before the terms output.
		'append' => '', // Text to be added after the terms ouptut.
		'separator' => '· ', // Separator used to separate multiple terms.
		'taxonomy' => '', // Taxonomy name to fetch the terms from. Replace to set a default.
	);
	
	$atts = shortcode_atts( $defaults, $atts, 'bt-custom-post-meta' );
	
	// using get_the_term_list() to retrieve all the associated taxonomy terms for a taxonomy
	$bt_tax = get_the_term_list( $post->ID, $atts['taxonomy'], '', trim( $atts['separator'] ) . ' ', '' );
	
	$output = '<span class="entry-categories bt-categories">' . $atts['prepend'] . $bt_tax . $atts['append'] . '</span>';
	
	// Allow a filter to change the default output and the shortcode attributes/arguments
	return apply_filters( 'bt_custom_post_meta', $output, $atts );
}

Once you’ve added this code to your functions.php, you can use this shortocde [bt-custom-post-meta] with-in loop to display the associated custom taxonomy terms for the post.

Genesis users can directly use this shortcode in genesis_post_meta filter as shown here:

add_filter( 'genesis_post_meta', 'bt_display_post_meta' );
function bt_display_post_meta( $post_meta ) {
	$post_meta = '[bt-custom-post-meta taxonomy="category"]'; // Replace with the taxonomy you need to fetch the terms from.
	return $post_meta;
}
Posted In:

UX & Frontend Design, Code Snippets, Genesis WordPress Theme Framework Child-Theme

1 Comment

  1. Thank you for writing this. It seems like the perfect solution for my use case, however I’m having issues with the actual terms not displaying on the front end. I’ve used the CPT UI plugin to generate a custom post type of Beer with custom Taxonomies of Style and Availability. I’m trying to display style terms via the shortcode and nothing’s showing on the front end. I’ve made the following change to set “style” as the default taxonomy.

    ‘taxonomy’ => ‘style’,

    The shortcode is partially working because I do see the “Listed Under:” text on the front end. Am I missing something else?

    Thanks again

    Chris

    Reply

Comment on Genesis Tip — How To Display Custom Taxonomy Terms In Post Meta

Your email address will not be published. Required fields are marked *