2016-01-30 72 views
1

我在page.php文件中的代碼,創建子頁面的列表。我希望每個li都有一個由精選圖像功能添加的背景圖像。下面是完整的代碼,我有入門特色圖片作爲背景圖像

<?php while (have_posts()) : the_post(); ?> 
<?php get_template_part('content', 'page'); ?> 
<?php endwhile; // end of the loop. ?> 

<?php 
if (is_page('eventsphotography')) { 
$query = new WP_query('pagename=eventsphotography'); 
$eventsphotography_id = $query->queried_object->ID; 

//The loop 
if($query->have_posts()) { 
    while($query->have_posts()) { 
     $query->the_post(); 
     the_content(); 
    } 
} 

/* Get the children of the eventsphotography page */ 
$args = array (
    'post_parent' => $thePostID, 
    'post_parent' => $eventsphotography_id, 
    'order' => 'ASC' 
); 
$eventsphotography_query = new WP_query($args); 
//The Loop 
if($eventsphotography_query->have_posts()) { 
    echo '<ul class="events-list">'; 
    while($eventsphotography_query->have_posts()){ 
     $eventsphotography_query->the_post(); 
     $background = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); 
     echo '<li style="background:url(' . $background[0] . '); background-repeat:no-repeat; background-size:cover;">'; 
     echo '<div class="events-centered">'; 
     echo '<a href="' . get_permalink() . '">'; 
     echo '<h4>' . get_the_title() . '</h4>'; 
     echo '</a>'; 
     echo '<div class="view-events-details">'; 
     echo '<a href="' . get_permalink() . '">'; 
     echo '<h5>View Images</h5>'; 
     echo '</a>'; 
     echo '</div>'; 
     echo '</div>'; /* end of events-centered */ 
     echo '</li>'; 
    } 
    echo'</ul>'; 
} 
} 
?> 

我只需要幫助這些行:

$background = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); 

echo '<li style="background:url(' . $background[0] . '); background-repeat:no-repeat; background-size:cover;">'; 

這裏是我的代碼結果的屏幕截圖: http://oi68.tinypic.com/10xzdhl.jpg

我用紅色矩形標記了第一個<li>。正如我以前說過,我要精選圖像的URL被放置在<li style="background:url(URL of the featured image)">

+0

不應該'$ background [0]'只是'$ background'? – RST

+0

風格是背景圖像:src不是背景。 – WisdmLabs

+0

或經過$ url = wp_get_attachment_url(get_post_thumbnail_id($ post-> ID)); WisdmLabs

回答

0

我已經找到了解決辦法。首先,我創建了一個new WP_Query

$subs = new WP_Query(array('post_parent' => $post->ID, 'post_type' => 'page', 'meta_key' => '_thumbnail_id'));

然後在我的循環添加此行:

if($eventsphotography_query->have_posts() && $subs->have_posts()) { echo '<ul class="events-list">'; while($eventsphotography_query->have_posts() && $subs->have_posts()){ $eventsphotography_query->the_post(); $subs->the_post(); echo '<li>'; echo get_the_post_thumbnail($post->ID);

...代碼的其餘部分...