0

How can I display the featured image of a post in a modal along with it's content.

With the help of a few online threads on similar issues, I tried this:

<?php
    if ( has_post_thumbnail()) {
        echo '<a href="' . get_permalink($post->ID) . '" >';
            the_post_thumbnail('my_feature_image', array( 'class' => "someName" ));
        echo '</a>';
    }
?>

Which unfortunately returns the same featured image for all posts.

On the same header.php where the modal is found, the following is there (above the modal divs):

$perma = false; 

if ( isset( $wp_query->query_vars['phpost_slug'] ) ) #WHFIX 24/03/2015: 
    $perma = $wp_query->query_vars['phpost_slug'];

if( $perma ) {
    //we don't want to return a 404
    $wp_query->set( 'is_404', false );
    $phid = get_page_by_path( $perma, OBJECT, 'post' );
    $postvote = get_post_meta( $phid->ID, 'epicredvote', true );
    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $phid->ID ), 'single-post-thumbnail' );

    $pluginfeat = get_post_meta( $phid->ID, 'phog', true );
    $desc = get_post( $phid->ID )->post_content;

link to site: https://goo.gl/30a3QQ [Click on the post's row to open the modal.]

6
  • Can you post the context of this code i.e. your loop/template Sep 19, 2015 at 10:10
  • Please post full code. Also if you are using this in loop then it must work fine.
    – Robert hue
    Sep 19, 2015 at 10:35
  • The above when kept in the index.php works beautifully for the post thumbnails as you can see here: reignofmorons.com
    – AndrewL64
    Sep 19, 2015 at 10:51
  • However, in header.php, I kept the above within a modal and it doesn't seem to work. Let me check what loop is used as I didn't initially made the page. Hold on.
    – AndrewL64
    Sep 19, 2015 at 10:52
  • @TheDeadMedic Please check updated answer
    – AndrewL64
    Sep 19, 2015 at 11:20

1 Answer 1

1

It seems you are using this outside the loop then use get_the_post_thumbnail to get the featured image for any post by supplying post id.

So your code will become this.

<?php
    if ( has_post_thumbnail() ) {
        echo '<a href="' . get_permalink( $post->ID ) . '" >';
            echo get_the_post_thumbnail( $post->ID, 'my_feature_image', array( 'class' => "someName" ) );
        echo '</a>';
    }
?>
2
  • Tried your version just now and still the same thing unfortunately :(
    – AndrewL64
    Sep 19, 2015 at 17:36
  • Ok here's the weird part. I replaced this line: echo '<a href="' . get_permalink( $post->ID ) . '" >'; with this echo '<a href="#" >'; and it has now changed but to another post's featured image for all posts.
    – AndrewL64
    Sep 19, 2015 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.