Displaying CPT taxonomy terms in Genesis entry footer

Posted on

In Genesis Slack chat, a user asked:

I installed Portfolio Post Type plugin. I am unable to understand why the footer `entry-meta` does not show up?

By default, linked taxonomy terms do not appear in the entry footer in Genesis for CPTs, both in single and archive views.

We can use [post_terms] shortcode inside a function hooked to genesis_post_meta to show these.

// Show Portfolio Type hyperlinked terms (categories and tags)
add_filter( 'genesis_post_meta', 'sk_post_meta_filter' );
function sk_post_meta_filter( $post_meta ) {

    if ( 'portfolio' == get_post_type() ) {
        $post_meta = '[post_terms taxonomy="portfolio_category"][post_terms taxonomy="portfolio_tag" before="Tagged With: "]';
    }

    return $post_meta;

}

where portfolio is the CPT with associated portfolio_category and portfolio_tag custom taxonomies.

If you’d like to replace the common class, entry-terms with entry-categories and entry-tags for your taxonomies, in this case, say for portfolio_category and portfolio_tag respectively, we can create a custom shortcode based off of post_terms and achieve this like so:

add_shortcode( 'portfolio_terms', 'custom_portfolio_terms_shortcode' );
/**
 * Produces the linked post taxonomy terms list.
 *
 * Supported shortcode attributes are:
 *   after (output after link, default is empty string),
 *   before (output before link, default is 'Tagged With: '),
 *   sep (separator string between tags, default is ', '),
 *    taxonomy (name of the taxonomy, default is 'category').
 *
 * Output passes through `genesis_post_terms_shortcode` filter before returning.
 *
 * @since 1.6.0
 *
 * @param array|string $atts Shortcode attributes. Empty string if no attributes.
 * @return string Output for `post_terms` shortcode, or empty string on failure to retrieve terms.
 */
function custom_portfolio_terms_shortcode( $atts ) {

	$defaults = array(
		'after'    => '',
		'before'   => __( 'Filed Under: ', 'genesis' ),
		'sep'      => ', ',
		'taxonomy' => 'category',
	);

	/**
	 * Post terms shortcode defaults.
	 *
	 * Allows the default args in the post terms shortcode function to be filtered.
	 *
	 * @since 2.0.0
	 *
	 * @param array $defaults The default args array.
	 */
	$defaults = apply_filters( 'genesis_post_terms_shortcode_defaults', $defaults );

	$atts = shortcode_atts( $defaults, $atts, 'post_terms' );

	$terms = get_the_term_list( get_the_ID(), $atts['taxonomy'], $atts['before'], trim( $atts['sep'] ) . ' ', $atts['after'] );

	if ( is_wp_error( $terms ) )
			return '';

	if ( empty( $terms ) )
			return '';

	if ( genesis_html5() )
		switch ( $atts['taxonomy'] ) {
			case 'portfolio_tag':
				$output = sprintf( '', genesis_attr( 'entry-tags' ) ) . $terms . '';
				break;

			default:
				$output = sprintf( '', genesis_attr( 'entry-categories' ) ) . $terms . '';
				break;
		}
	else
		$output = '' . $terms . '';

	return apply_filters( 'genesis_post_terms_shortcode', $output, $terms, $atts );

}

// Show Portfolio Type hyperlinked terms (categories and tags)
add_filter( 'genesis_post_meta', 'sk_post_meta_filter' );
function sk_post_meta_filter( $post_meta ) {

	if ( 'portfolio' == get_post_type() ) {
		$post_meta = '[portfolio_terms taxonomy="portfolio_category"][portfolio_terms taxonomy="portfolio_tag" before="Tagged With: "]';
	}

	return $post_meta;

}