How can I make thumbnails a display different sizeposition if the blog post was posted before a specific date?

I would like to be able to display the blog post's thumbnail a smaller size and right floated if it was posted befo

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?

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?

Share Improve this question edited Jan 31, 2020 at 7:36 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jan 30, 2020 at 20:06 Brenda CareyBrenda Carey 12 bronze badges 1
  • reverse the comparison; and use get_the_post_thumbnail() developer.wordpress/reference/functions/… in the string concatenation, and remove the image tags because the function returns the complete image tag for the featured image. – Michael Commented Jan 31, 2020 at 4:43
Add a comment  | 

1 Answer 1

Reset to default -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() . '" />';
        }
    }
?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744788023a4593744.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信