DPS Default Image

When using Display Posts Shortcode, this code snippet will let you specify a default image to use like this:

[display-posts image_size="thumbnail" default_image="123"]

Where “123” is the ID of the image you’d like to use as your default.

<?php //don't include this line
/**
* DPS default image
* @see https://www.billerickson.net/code/dps-default-image/
*/
function be_dps_default_image( $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class, $author, $category_display_text ) {
if( !empty( $original_atts['image_size'] ) && !empty( $original_atts['default_image'] ) && empty( $image ) ) {
$default_image_id = intval( $original_atts['default_image'] );
if ( $original_atts['include_link'] ) {
$image = '<a class="image" href="' . get_permalink() . '">' . wp_get_attachment_image( $default_image_id, $original_atts['image_size'] ) . '</a> ';
} else {
$image = '<span class="image">' . wp_get_attachment_image( $default_image_id, $original_atts['image_size'] ) . '</span> ';
}
if( !empty( $image ) )
$output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $date . $author . $category_display_text . $excerpt . $content . '</' . $inner_wrapper . '>';
}
return $output;
}
add_filter( 'display_posts_shortcode_output', 'be_dps_default_image', 10, 11 );
view raw functions.php hosted with ❤ by GitHub

Display Posts Shortcode

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