How to display a list of CPT entries below single CPT pages in Genesis

Posted on

In Genesis Slack chat‘s #beginners room, a user asked:

I want to add a list of posts to the bottom of my single-cpt page. In the past, I’ve created a new sidebar and added it to a widget. Is there a better way? Ideally, it would not include the current post.

What other plugin for this than the excellent Display Posts Shortcode by Bill Erickson?

Adding the following sample code in child theme’s functions.php after activating Display Posts Shortcode will display linked titles of 5 most recent entries of a particular kbitem post type below single pages of that post type while ensuring that the current post is not shown.

// Display Posts Shortcode below the content on single 'kbitem' CPT entries
add_action( 'genesis_after_loop', 'sk_display_more_entries' );
function sk_display_more_entries() {

	if ( ! is_singular( 'kbitem' ) ) {
		return;
	}

	echo do_shortcode( '[display-posts post_type="kbitem" posts_per_page="5" title="More Knowledge Base Items"]' );

}

/**
 * Display Posts - Exclude Current Post
 *
 * @author Bill Erickson
 * @link http://wordpress.org/extend/plugins/display-posts-shortcode/
 *
 * @param array $args
 * @return array
 */
function be_exclude_current_post( $args ) {
	if( is_singular() && !isset( $args['post__in'] ) )
		$args['post__not_in'] = array( get_the_ID() );

	return $args;
}
add_filter( 'display_posts_shortcode_args', 'be_exclude_current_post' );

References: https://wordpress.org/support/topic/plugin-display-posts-shortcode-not-to-include-current-id#post-2830775

https://github.com/billerickson/display-posts-shortcode/wiki

Note: I also added the following in my test site’s child theme (Genesis Sample) style.css:

.display-posts-listing {
	background-color: #fff;
	padding: 40px;
	margin-bottom: 40px;
}