css - 如何显示特色图片?

标签 css wordpress background-image

我只是想在我的 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/

相关文章:

html - 使用 "style"属性定义的背景图像未在 IE 中显示

css - :before and background-image. .. 它应该工作吗?

javascript - Chrome - 文本不在其元素边界内?

javascript - 如何使用javascript更新select标签的值,这些值可以是多个值

css - 如何锁定双击到 WooCommerce 中的变体

javascript - 在页面末尾打开对话框时窗口滚动开始

php - Wordpress 表单处理

php - ACF - 通过帖子循环,然后通过灵活的内容,然后通过转发器

php - 转换为 sprintf() 查询

html - 具有不同 z-index 的同一 css 类中的多个背景图像