Skip to content

Instantly share code, notes, and snippets.

@BruceMcKinnon
Created May 7, 2019 05:44
Show Gist options
  • Save BruceMcKinnon/299aa9924a87c997ea460e576810095b to your computer and use it in GitHub Desktop.
Save BruceMcKinnon/299aa9924a87c997ea460e576810095b to your computer and use it in GitHub Desktop.
Content Block Featured Image
// Add the ability to display the content block in a reqular post using a shortcode
function custom_post_image_shortcode( $atts ) {
$params = shortcode_atts( array(
'id' => '',
'slug' => '',
'class' => 'content_block_image',
'suppress_content_filters' => 'no'
), $atts );
if ( $params["slug"] ) {
$block = get_page_by_path( $params["slug"], OBJECT, 'content_block' );
if ( $block ) {
$params["id"] = $block->ID;
}
}
$content = "";
if( $params["id"] != "" ) {
$args = array(
'post__in' => array( $params["id"] ),
'post_type' => 'content_block',
'class' => 'content_block_featured'
);
$content_post = get_posts( $args );
$inline_style = "";
foreach( $content_post as $post ) {
if ( has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
$image = $image[0];
$inline_style = 'style="background-image: url('.$image.')"';
}
$content .= '<div class="'. esc_attr( $params["class"] ). '" ' . $inline_style . '></div>';
}
}
return $content;
}
add_shortcode( 'content_block_featured_image', 'custom_post_image_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment