dsf5989 2016-01-30 11:06
浏览 85
已采纳

将特色图像作为背景图像

I have a code in my page.php file that creates a list of child pages. I want every li to have a background-image added by Featured image function. Here is the entire code I have

<?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>';
}
}
?>

I only need help for these lines:

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

AND

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

Here's the screenshot of the result of my code: http://oi68.tinypic.com/10xzdhl.jpg

I marked the first <li> with a red rectangle. As I said before, I want URL of the featured image to be placed in <li style="background:url(URL of the featured image)">

  • 写回答

1条回答 默认 最新

  • dpyoh6553 2016-01-31 13:08
    关注

    I have found a solution. First, I created a new WP_Query:

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

    Then in my loop I added this lines:

    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);

    ...rest of the code...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常