Wordpress发布缩略图问题


Wordpress post thumbnail problems

除了帖子缩略图,我现在使用的是一个占位符图像,效果很好,但希望使用帖子中的特色图像作为背景,这是我的代码。。。

<?php
$args = array( 'posts_per_page' => 10, 'category' => 3 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
        <div class="box" style="background-image:url('<?php the_post_thumbnail(); ?>');">
            <div class="overlay">
                <div class="buttonContainer">
                    <a href="<?php the_permalink(); ?>">
                        <button>preview</button>
                    </a>
                </div>
            </div>
        </div>

<?php endforeach; 
wp_reset_postdata();?> 

当我在浏览器中查看这个时,它会打印出这个。。。');">且无图像。。。我哪里出了问题?

post_thumbnail()返回一个img元素,类似于;

<img src="some-image-url' />

这不是您想要传递到背景图像的内容。

你需要这样的东西:

$attachment_id= get_post_thumbnail_id( the_ID() );
$img_data = wp_get_attachment_image_src( $attachment_id, the size you want );

则可以回显$img_data[0]以获取附件URL。