I would like to “upvote” this as well. I wrote my own shortcode function based on yours to include this and some other improvements in the attribute handling (see this helpful thread).
See what you think (and feel free to use all or some):
function my_custom_post_widget_shortcode( $atts ) {
$args = shortcode_atts( array(
'id' => '-1',
'slug' => '',
'class' => 'content_block',
'suppress_content_filters' => 'no',
'title' => 'no',
'title_tag' => 'h2'
), $atts, 'custom_post_widget_shortcode' );
$suppress_content_filters = filter_var($args['suppress_content_filters'], FILTER_VALIDATE_BOOLEAN);
$show_title = filter_var($args['title'], FILTER_VALIDATE_BOOLEAN);
$query_args = array(
'post_type' => 'content_block'
);
if ( !empty($args['slug']) ) {
$query_args['name'] = $args['slug'];
} else {
$query_args['p'] = $args['id'];
}
$output = "";
$content_post = get_posts( $query_args );
foreach( $content_post as $post ) :
$output .= '<div class="'. esc_attr($args['class']) .'" id="custom_post_widget-' . $id . '">';
if ($show_title) {
$output .= '<'.$args['title_tag'].'>';
if ($suppress_content_filters) {
$output .= $post->post_title;
} else {
$output .= apply_filters('the_title', $post->post_title);
}
$output .= '</'.$args['title_tag'].'>';
}
if ( $suppress_content_filters ) {
$output .= $post->post_content;
} else {
$output .= apply_filters( 'the_content', $post->post_content);
}
$output .= '</div>';
endforeach;
return $output;
}
For anyone who would like to use this, simply override the shortcode in your own plugin or theme. I found the ‘wp_loaded’ action worked fine, but will admit there may be a better choice – you just have to make sure the plugins are loaded and activated:
function override_cpw_shortcode(){
add_shortcode( 'content_block', 'my_custom_post_widget_shortcode' );
}
add_action('wp_loaded', 'override_cpw_shortcode');