2017-06-22 52 views
0

我正在尝试获取单个帖子的精选图像的图像位置。我的代码如下使用wp_get_attachment_image_url()什么也没有返回。任何想法我在这里做错了吗?由于从页面获取精选图像网址

<?php 
    $we_query = new WP_Query(array('post_status' => draft, 'page_id' => $wp_page_id)); 
    while ($we_query->have_posts()) : $we_query->the_post(); 
     if (has_post_thumbnail()) { 
      echo '<div class="buying-hero__image" style="background: rgba(0, 0, 0, 0) url(&quot;'.wp_get_attachment_image_url($wp_page_id, 'full').'")></div>'; 

     } 
     echo '<div class="container">'; 
     the_content(); 
     echo '</div>'; 

    endwhile; wp_reset_query(); 

    ?> 
+0

的特色是什么形象?个人帖子?那么你为什么要传递一个页面ID作为第一个参数...? – CBroe

+0

@CBroe是页面的个别文章 – user2636556

回答

0

你需要使用一个名为get_post_thumbnail_id功能一起命名wp_get_attachment_image_src功能。这将返回一个数组,其中需要第一个元素。

<?php 
while ($we_query->have_posts()) : 
    $we_query->the_post(); 
    if (has_post_thumbnail()) { 
     $image = wp_get_attachment_image_src(get_post_thumbnail_id($wp_page_id), 'full'); 
     echo '<div class="buying-hero__image" style="background: rgba(0, 0, 0, 0) url(\"' . $image[0] . '\")></div>'; 
    } 
    echo '<div class="container">'; 
    the_content(); 
    echo '</div>'; 
endwhile; 
+0

$ image [0]给我这个'

' – user2636556

+0

我忘了在'url()'中使用双引号('''),我更新了我的答案,现在;) – roberto06

+0

即时通讯仍然得到这个输出'

' – user2636556