How to show featured image instead of post content?

Discussion in 'WordPress' started by Divvy, Jun 18, 2019.

  1. #1
    Hello guys,

    Maybe you can help me with a little thing...

    I have a code to show a video, and when I don't have a video, the post content is shown instead. I need to replace the post content (text) with the post featured image: the_post_thumbnail('full');

    This is my code:

    
    <div id="play_video">
    
    <?php $content=get_post_meta($videoId,"wpscript_embed",true);
    if($content){                 
    $width = '100%';
    $content = preg_replace('/width="(.*?)"/i', 'width="' . $width .'"', $content);
    $content = preg_replace('/src="http:/i', 'src="https:', $content);
    echo $content; 
    }else{
    echo apply_filters('the_content', get_post_field('post_content', $videoId));
    }
    ?>
    
    <script type="text/javascript">
    
    <?php 
    $id=get_post_thumbnail_id( $videoId );
    ?>
    
    var poster = '<?php echo  wp_get_attachment_url( $id ); ?>';
    var x = document.getElementsByClassName("wp-video-shortcode");
    if(x[0] != 'undefined' || x[0]!=''){
    x[0].setAttribute('poster', poster);
    }
    </script> 
    
    </div>
    PHP:
    I think I only need to change this line:
    echo apply_filters('the_content', get_post_field('post_content', $videoId));
    PHP:
    If I replace the line above with this one:
    the_post_thumbnail('full');
    PHP:
    It works... but it shows the image in both posts, with videos, and without videos.
    I only want to show in posts without videos.

    Can someone help me? :)

    Thanks in advance.
     
    Divvy, Jun 18, 2019 IP