skip to Main Content

I’m using this code to get featured image from post. This is my whole code every thing else is working rather than the image i’m trying to display featured image also the posttype i’m using is custom post type.
Can anyone tell what I’m doing wrong?

    add_shortcode('get-video-post-type','videos_cpt');
function videos_cpt(){
    $args = array(
        'post_type' => 'Videos',
        'post_status'=>'publish',
    );
    $result = new WP_Query($args);
    if ($result -> have_posts()){
        while($result -> have_posts()){
            $result -> the_post();
        ?>
        <div id="show-all-post" class="posts-carousel">
            <div class="item" id="video-box">
                <div class="left-img"><img class="post-image" src="<?php echo get_the_post_thumbnail( get_the_ID() , 'full' ); ?>" alt="image"></div> 
                <div class="right-content">
                    <h1 style="color:black;"><?php the_title();?></h1>
                    <p style="color:black;"><?php echo substr(get_the_excerpt(), 0,50); ?>....</p>
                    <a href="<?php the_permalink();?>">Read More</a>
                </div>
            </div>
        </div>
        <?php 
        }
    }
    wp_reset_postdata();
}

3

Answers


  1. You can Try this one:

    <img class="post-image" src="<?php echo get_the_post_thumbnail_url(get_the_ID(),'full');?>" alt="image">
    
    Login or Signup to reply.
  2. Please add **echo** with get_the_post_thumbnail() function and add get_the_ID() instead of $post_ID variable.
    
    <img class="post-image" src="<?php echo get_the_post_thumbnail( get_the_ID() , 'full' ); ?>" alt="image">
    
    It will be defiantly work. It is working in my WordPress setup. 
    
    Check with this reference link:
    https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
    
    Login or Signup to reply.
  3. Can you try this:

    <?php $bg = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );?>
    <div class="left-img"><img class="post-image" src="<?php echo $bg[0];?>" alt="image"></div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search