【问题标题】:How to change background image with post thumbnail function WordPress如何使用后期缩略图功能 WordPress 更改背景图像
【发布时间】:2017-02-09 05:36:28
【问题描述】:

我正在创建一个 WordPress 主题。我的 CSS 中有一个带有背景图像属性的部分。示例

我的 HTML

 <div class="header"></div>

我的 CSS

.header{
   background-image:url('../path/images/image.jpg');
}

现在我想让这个部分动态化。因此用户可以更改他们的背景图像。 现在我试图使用post_thumbnail() 函数来更改我的图像。但我发现,我们只能将它用于&lt;img/&gt; 标签。

wordpress中有什么方法或功能可以用来改变背景图片吗?

【问题讨论】:

  • 如果是为了 css 为什么不尝试内联样式
  • 是的,为了动态背景,我们必须内联样式。检查下面的代码。我已经回答过了。 @丹尼尔

标签: php css wordpress


【解决方案1】:

试试这个。它对我有用。 :)

<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>

<div class="header" style="background-image: url('<?php echo $thumb['0'];?>')">
</div>

【讨论】:

    【解决方案2】:

    在css中不行,你必须写在php页面或模板中。

    请看下面的代码:

    <div style="background-image: url(<?php echo get_the_post_thumbnail_url($post->ID); ?>)" class="header"></div>
    

    这里,'$post->ID' 是帖子/页面的 ID。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2015-07-15
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      相关资源
      最近更新 更多