0

i'm working on a WordPress plugin and i'm trying to display/get the post thumbnail below the post on my "Related posts" part. I tried many things which are supposed to work but not for me ... here is the code (on my plugin.php file):

add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );

if ( has_post_thumbnail() ) {
    echo ('there is a thumbnail');
    echo get_the_post_thumbnail(get_the_ID(), "thumbnail"); // nothing
    echo get_the_post_thumbnail(); // nothing
    echo var_dump(get_the_post_thumbnail(get_the_ID(), "thumbnail")); //string '' (length=0)
    echo var_dump(get_the_post_thumbnail()); //string '' (length=0)
    the_post_thumbnail('thumbnail'); // nothing
    the_post_thumbnail(); // nothing
}
else {
    echo ('there is no thumbnail');
}

first i thought that there were no thumbnail for my post but my first echo returned "there is a thumbnail" and all the rest doesn't return anything ... so i don't understand... what is missing ?

Thanks

Edit:

here is the current full code in my plugin.php file:

function Similar_Article ($content) {
    if (is_single() || is_page()) {
        the_post();
        $similar_to_add = '';
        $id = get_the_ID();
        $content_and_similar = $content . '<br><br>'; //spaces between post and related posts

        /* many methods called to find my related posts */

        if (has_post_thumbnail()){
            $content_and_similar .= get_the_post_thumbnail(); // it works
        }
        else {
            $imageData = wp_get_attachment_image_src(get_post_thumbnail_id ( $id ), 'thumbnail'); 
            $content_and_similar .= '<img src=" '.$imageData[0].' ">';
        }

        $content_and_similar .= $similar_to_add;
        return $content_and_similar;
    }
    else {
        return $content;
    }
}

add_filter( 'the_content', 'Similar_Article' );

my function works if there is a thumbnail but display nothing if there is no thumbnail...

0

Please notice, has_post_thumbnail() does not only check to see if the post has a featured image, but also checks if there is an image in the post itself. There is a quote from WordPress Code Reference:

It’s worth noting that has_post_thumbnail() does not just check for the Featured Image as the Codex User Contributed Note suggests. If a post contains no defined featured image but it does contain an image in the content this function will still return TRUE.

I suggest you use if (get_the_post_thumbnail() != '') instead of if (has_post_thumbnail()).

| improve this answer | |
  • thx for your answer, but I don't really understand this "Loop", I want to display my related posts thumbnail below a specific post, the one that is opened .. so why this loop on all posts ? – Cédric Cosson Jun 12 '17 at 10:19
  • The reason is explained here. You don't really need the loop since i already check if we are on a single page (is_singluar()), but the important thing is that you have to setup the post data before you can access it. – Jack Johansson Jun 12 '17 at 10:24
  • hum ok for the loop, but I want to display something even if there is no thumbnail set ... so i tried that if (has_post_thumbnail()){ echo get_the_post_thumbnail(); } else { $imageData = wp_get_attachment_image_src(get_post_thumbnail_id ( get_the_ID(), 'thumbnail'); ?> <img style="float:left; padding-right: 35px;" src="<?php echo $imageData[0]; ?>"/> <?php } but it's not working ... – Cédric Cosson Jun 12 '17 at 10:42
  • @CédricCosson Are you directly using the code in your plugin.php file? or wrapped in a function and then calling the function in your single.php? – Jack Johansson Jun 12 '17 at 10:56
  • yes i use the code directly inside my plugin.php file, I can't write directly in my single.php file because i'm creating a plugin and i need that all i do stay in my plugin directory ... but maybe there is a hook that i can use ? Currently I have a function that get the post content and add my related post below and then, I hook it using add_filter( 'the_content', 'my_function' ); is that wrong ? – Cédric Cosson Jun 12 '17 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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