Home > Back-end >  how to get featured image from custom post in wordpress?
how to get featured image from custom post in wordpress?

Time:11-24

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" >
            <div  id="video-box">
                <div ><img  src="<?php echo get_the_post_thumbnail( get_the_ID() , 'full' ); ?>" alt="image"></div> 
                <div >
                    <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();
}

CodePudding user response:

Please add **echo** with get_the_post_thumbnail() function and add get_the_ID() instead of $post_ID variable.

<img  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/

CodePudding user response:

You can Try this one:

<img  src="<?php echo get_the_post_thumbnail_url(get_the_ID(),'full');?>" alt="image">
  • Related