Using the Display Posts Shortcode Output Filter

The output filter lets you add, remove, edit, and re-order the elements that make up each post listing. Here are some examples.

Moving the image after the title

/**
 * Display Posts Shortcode - Move image after title
 * @link https://www.billerickson.net/code/using-display-posts-shortcode-output-filter
 *
 */
function be_dps_move_image_after_title( $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class, $author, $category_display_text ) {
  $output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $title . $image . $date . $author . $category_display_text . $excerpt . $content . '</' . $inner_wrapper . '>';
  return $output;
}
add_filter( 'display_posts_shortcode_output', 'be_dps_move_image_after_title', 10, 11 );

Moving the date after the content

/**
 * DPS - Move date after content 
 * @link https://www.billerickson.net/code/using-display-posts-shortcode-output-filter
 *
 */
function be_dps_move_date_after_content( $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class, $author, $category_display_text ) {
  $output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $author . $category_display_text . $excerpt . $content . $date . '</' . $inner_wrapper . '>';
  return $output;
}
add_filter( 'display_posts_shortcode_output', 'be_dps_move_date_after_content', 10, 11 );

Copy this line from the plugin, then move the elements around as you see fit, then return the output.

Add Time

/**
 * Add Time to Display Posts Shortcode
 * @author Bill Erickson
 * @link https://www.billerickson.net/code/add-time-to-display-posts-shortcode/
 *
 * @param $output string, the original markup for an individual post
 * @param $atts array, all the attributes passed to the shortcode
 * @param $image string, the image part of the output
 * @param $title string, the title part of the output
 * @param $date string, the date part of the output
 * @param $excerpt string, the excerpt part of the output
 * @param $inner_wrapper string, what html element to wrap each post in (default is li)
 * @param $content string, post content
 * @param $class array, post classes
 * @return $output string, the modified markup for an individual post
 */
function be_display_posts_time( $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class, $author, $category_display_text ) {
	// Find out if 'include_time=true' was added to  the shortcode
	$time = '';
	if( isset( $atts['include_time'] ) )
		$time = ' at ' . get_the_time( 'g:i a' );
	// Now let's rebuild the output and add the $time to it
	$output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $date . $time . $author . $category_display_text . $excerpt . $content . '</' . $inner_wrapper . '>';
	// Finally we'll return the modified output
	return $output;
}
add_filter( 'display_posts_shortcode_output', 'be_display_posts_time', 10, 11 );

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk