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;
}
Divi WordPress Theme