在其他页面上获取特色图片

时间:2019-02-01 12:06:32

标签: php wordpress thumbnails

我想在页面上显示一些自定义帖子及其特色图片。问题是所有帖子div上都显示了第一篇文章的图像。

这就是我的代码

<?php
//solutions
$args = array(   
    'showposts'=>-1,
    'category_name' => 'Spage',
    'order' => 'ASC',
); 
$query = new WP_Query( $args ); 
$asPost = array();
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        $asPost[] = array('title' => $query->post->post_title,
                                   'content' => $query->post->post_content);
    }
}
?>

<?php $count = 0; ?>
        <?php if(!empty($asPost)){ ?>
             <?php foreach($asPost as $item){ ?>
                    <div class="container box<?php if( $count%3 == 0 ) { echo '-1'; }; $count++; ?>">
                        <div class="item">
                            <p class="headline font--h5 accent--teal"><?php echo $item['title']; ?></p>
                            <div class="fullwidth">
                                <?php the_post_thumbnail(); ?>
                            </div>
                            <div class="font--h5 body body--dark">
                                <?php echo apply_filters('the_content',$item['content']);?>
                            </div>
                        </div>
                    </div>
            <?php }?>
        <?php }?> 

如何拍摄正确的图像?

1 个答案:

答案 0 :(得分:2)

尝试此代码。我修改了这些'id'=>$query->post->ID<?php get_the_post_thumbnail($item['id']); ?>代码。

$args = array(   
    'showposts'=>-1,
    'category_name' => 'Spage',
    'order' => 'ASC',
); 
$query = new WP_Query( $args ); 
$asPost = array();
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        $asPost[] = array('title' => $query->post->post_title,
                                   'content' => $query->post->post_content, 'id'=>$query->post->ID);
    }
}

?>

<?php $count = 0; ?>
        <?php if(!empty($asPost)){ ?>
             <?php foreach($asPost as $item){ ?>
                    <div class="container box<?php if( $count%3 == 0 ) { echo '-1'; }; $count++; ?>">
                        <div class="item">
                            <p class="headline font--h5 accent--teal"><?php echo $item['title']; ?></p>
                            <div class="fullwidth">
                                <?php get_the_post_thumbnail($item['id']); ?>
                            </div>
                            <div class="font--h5 body body--dark">
                                <?php echo apply_filters('the_content',$item['content']);?>
                            </div>
                        </div>
                    </div>
            <?php }?>
        <?php }?>