How can a shortcode can take the place of the featured image in a post?

When there is a specific shortcode in a post (example [sc]anything[/sc]), I want to hide the featured and take it’s place. How can I do that?

I add this on my theme function

Read More
// TopWide Shortcode
function wpsite_topwide( $atts, $content = null ){
    return '<div class="topwide">' . do_shortcode($content) . '</div>';
}
add_shortcode( 'topwide', 'wpsite_topwide' );

/** 
 * Find if content has topwide shortcode in it. 
 */ 
function wpsite_has_topwide_shortcode() { 
    global $post; 

    if ( ! is_single() ) 
        return; 

    if ( false === strpos( $post->post_content, '[topwide' ) ) 
        return false; 

    return true; 
}

and this on single post

<?php
if (wpsite_has_topwide_shortcode() && has_post_thumbnail() ) { 
    echo '<div class="topwide">' . get_the_post_thumbnail( $post->ID, 'full' ) . '</div>'; 
} 
?>

But doesn’t takes the place of the featured image in the post…

Related posts

Leave a Reply

1 comment

  1. Apparently you are simply not translating your logic into code:

    • if it has shortcode, do this,
    • if not has shortcode but has thumbnail, do that,
    • otherwise, do something else.
    if ( wpsite_has_topwide_shortcode() ) { 
        echo 'shortcode';
    } elseif( has_post_thumbnail() ) {
        echo get_the_post_thumbnail( $post->ID, 'full' );
    } else {
        echo 'Hello World';
    }