【问题标题】:Wordpress thumbnail from category as background div类别中的 Wordpress 缩略图作为背景 div
【发布时间】:2014-06-20 05:54:41
【问题描述】:

我在使用 Wordpress 时遇到了问题。

我希望我上一篇文章的缩略图作为我的 div 的背景。

代码:

<div<?php

    if ( $thumbnail_id = get_post_thumbnail_id() ) {
        if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
            printf( ' style="background-image: url(%s);"id="photopost"', $image_src[0] );     
    }

?><?php $cat_id = 3; //the certain category ID
$latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();  ?><?php endwhile; endif; ?>></div>

这适用于我上一篇文章(所有文章)的缩略图。 但我想要类别 ID 3 的最后一篇文章的缩略图。

我该如何解决这个问题?

谢谢, 比约恩

【问题讨论】:

    标签: php wordpress background thumbnails


    【解决方案1】:

    您在创建缩略图后调用查询。查询应该包装 div。

    试试:

    <?php $cat_id = 3; //the certain category ID
    $latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
    if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();  ?>
    
    <div<?php
    
        if ( $thumbnail_id = get_post_thumbnail_id() ) {
            if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
                printf( ' style="background-image: url(%s);"id="photopost"', $image_src[0] );     
        }
    
    ?>></div>
    
    <?php endwhile; endif; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      相关资源
      最近更新 更多