我只是想在我的 div 结构中的自定义 wordpress 主题中显示特色图片作为背景图片。
此处的新编辑是整个页面,因此您可以清楚地看到循环中有或没有的内容。我还检查了特色图片是否分配给了帖子,确实如此。我还检查了我的 functions.php,它也设置为使用缩略图。
<?php get_header(); ?>
<div class="page-section clear">
<div class="container clear">
<div class="news">
<div class="row">
<div class="col-md-4">
<div class="news-left">
<?php if (has_post_thumbnail( $post->ID )) :
?>
<?php $image = wp_get_attachment_image_src($post->ID); // returns an array ?>
<div class="news-image" style="background-image:url('<?php echo $image[0]; ?>');"></div>
<?php endif; ?>
</div>
</div>
<div class="col-md-8">
<main role="main">
<!-- section -->
<section>
<h1><?php echo get_the_title( $ID ); ?> </h1>
<?php get_template_part('loop'); ?>
<?php get_template_part('pagination'); ?>
</section>
<!-- /section -->
</main>
</div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
最佳答案
这可以通过 wp get attachment image src 来完成
<?php $image = wp_get_attachment_image_src($post->ID); // returns an array ?>
<div class="news-image" style="background-image:url('<?php echo $image[0]; ?>');"></div>
关于css - 如何显示特色图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26128957/