Dilan
Dilan

Reputation: 113

How to get WordPress post featured image from post ID in url

I'm trying to get the featured image from the postid passed through the url.

http://www.example.com/schedule-appointment/?postid=589

I've managed to get the postid from the url, but everything goes down hill from there. I must be missing something simple. I'm not a programmer...would love some help.

add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');

function cf7_add_post_id(){


        $Path=$_SERVER['REQUEST_URI'];                
        $control = array();
        $control = explode('?', $Path);       
        $get = $control[1];
        $get = explode('=', $get);
        $get2 = $get[1];
        $args = array(
            'post_type' => 'page',
            'post__in' => $get2,
        );

        // Fire up the Query
        $the_query = new WP_Query( $args );


        while ( $the_query->have_posts() ): $the_query->the_post();
        $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->$get2) );
        echo '$feat_image';


};

Upvotes: 1

Views: 3130

Answers (2)

hemnath mouli
hemnath mouli

Reputation: 2755

Try this

<?php
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');

function cf7_add_post_id(){
    $ID =   isset( $_GET["postid"] )    ?   $_GET["postid"] :   false;
    if( $ID ){
        $thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $ID ), 'full' );
        $url = $thumb['0'];
        echo "<img src ='".$url."' alt = 'Image'>";
    }
}
?>

Upvotes: 1

Mukesh Ram
Mukesh Ram

Reputation: 6328

There is no need for the WP_Query , You have one id and you can easily get this done by using following code,

 add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');

 function cf7_add_post_id(){


    $postid = $_GET['postid'];


    $feat_image = wp_get_attachment_url( get_post_thumbnail_id($postid) );
    echo '$feat_image';

};

Upvotes: 0

Related Questions