【问题标题】:What´s the error in this syntax?这个语法有什么错误?
【发布时间】:2015-01-04 18:24:45
【问题描述】:

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

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

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

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

')"&gt;

实时示例:WP Featured post image, as a div background

【问题讨论】:

    标签: html css wordpress background featured


    【解决方案1】:

    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] ?>')">
    

    【讨论】:

    • 抱歉,但我需要查看
      句。 3 小时后以这种方式工作,但出现问题并停止工作。我确定 $thumb_id = get_post_thumbnail_id(); $thumb_url = wp_get_attachment_image_src($thumb_id,'wpbs-featured', true); 不是必需的。
    • 我也会测试你的建议并呈现同样的结果
    • @VíctorBoero 您想将帖子特色图片加载为背景图片,那么您需要网址。如果您怀疑我的话,请查看 Wordpress 抄本,以了解 the_post_thumbnail 函数 codex.wordpress.org/Function_Reference/the_post_thumbnail
    【解决方案2】:

    这是实际的 html sn-p 工作。

    <?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>
    

    实时示例:Design Thinking. Blog de Diseño Gráfico. Picoboero

    【讨论】:

    • 我的答案正是您所需要的,无需发布另一个答案。祝你在下一个问题上得到我的帮助。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签