简体   繁体   English

WordPress发布缩略图问题

[英]Wordpress post thumbnail problems

I am all set apart from the post thumbnail, I am currently using a placeholder image which works fine but wish to use the featured image form the post as the background, here is my code... 除了帖子缩略图之外,我所有人都没有设置,我目前正在使用占位符图像,该图像效果很好,但希望使用帖子中的特色图像作为背景,这是我的代码...

<?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();?> 

When I view this in the browser it prints out this... ');"> and no image... Where do I appear to be going wrong? 当我在浏览器中查看此内容时,它会打印出此内容... ');">并且没有图像...在哪里出现问题?

the_post_thumbnail() returns an img element like; the_post_thumbnail()返回一个img元素,例如;

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

This is not what you want to pass to background-image. 这不是您想要传递给背景图像的内容。

You need something like: 您需要类似:

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

then you can echo $img_data[0] to get the attachment URL. 那么您可以回显$ img_data [0]以获取附件URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM