1

我正在尝试对我的 Bootstrap .post-title 类做一些事情。

我想在我的帖子标题上放置一个 element.style 背景,作为背景,用于每个帖子的帖子特色图像。我已经实现了这一点,但是出了点问题,现在无法正常工作。我唯一知道的是必须看起来像这样。

<div class="post-featured" style="background-image:url('upload/<?php the_post_thumbnail( 'wpbs-featured' ); ?>')">

但是语法中有一些错误,因为它会在 HTML 上呈现这些字符。这是怎么回事?

')">

现场示例:WP 精选帖子图片,作为 div 背景

4

2 回答 2

3

the_post_thumbnail返回一个 IMG html 标记。所以生成的代码是

<div class="post-featured" style="background-image:url('upload/<img src="path/to/file.png">')">

绝对不是可以工作的东西......你只想要网址,所以你应该这样做:

$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'wpbs-featured', true);

<div class="post-featured" style="background-image:url('<?= $thumb_url[0] ?>')">
于 2015-01-04T18:27:47.800 回答
-2

这是实际工作的 html 片段。

<?php
$img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "full");
$img = $img[0];
?>
<div class="postfeatured" style="<?php if($img){echo 'background:url('.$img.');';} ?>">
<div class="page-header"><h1 class="single-title" itemprop="headline"><?php the_title(); ?></h1>


  <h4> 
  <?php $mykey_values = get_post_custom_values('_aioseop_description');
  foreach ( $mykey_values as $key => $value ) {
  echo substr("$value",0 ,300); //This will display the first 150 symbols
  }?>
  </h4>


  <hr>
  <!-- Go to www.addthis.com/dashboard to customize your tools -->
  <div class="addthis_native_toolbox"></div>
  <hr>

  </div>

</div>

活生生的例子:设计思维。博客 de Diseño Grafico。皮科波罗

于 2015-01-04T19:58:46.837 回答