0

I would like to be able to display the blog post's thumbnail a smaller size and right floated if it was posted before April 11, 2019 but I can't seem to figure it out.

This is what I tried:

<?php
            global $post;
            $compare_date = strtotime( "2018-03-01" );
            $post_date    = strtotime( $post->post_date );

            if (has_post_thumbnail() && ( $compare_date < $post_date ) ) {
                echo '<div style="float:right;padding-left:20px;"><img src="'.the_post_thumbnail().'"  /></div>';}
            else {
                     echo '<img src="'.the_post_thumbnail().'" />';}
?>

But when I did this, it affected all of my post thumbnails rather than just the ones before a specific date.

Any ideas?

1
  • reverse the comparison; and use get_the_post_thumbnail() developer.wordpress.org/reference/functions/… in the string concatenation, and remove the image tags because the function returns the complete image tag for the featured image.
    – Michael
    Jan 31, 2020 at 4:43

1 Answer 1

-1

In what way is it affecting your post thumbnails?

I cleaned up your code a bit. If you can show me the output you're getting I'll be able to help you further.

<?php
    global $post;

    $compare_date = strtotime( "2018-03-01" );
    $post_date    = strtotime( $post->post_date );

    if ( has_post_thumbnail() ) {
        if ( $compare_date < $post_date ) {
            echo '<div style="float:right;padding-left:20px;"><img src="' . the_post_thumbnail() . '"/></div>';
        }
        else {
            echo '<img src="' . the_post_thumbnail() . '" />';
        }
    }
?>

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.