【问题标题】:Getting post featured image on different page在不同页面上获取帖子特色图片
【发布时间】:2019-06-26 00:18:02
【问题描述】:

我想在页面上显示一些自定义帖子及其特色图片。问题是第一个帖子的图像显示在所有帖子 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 }?> 

如何拍摄正确的图像?

【问题讨论】:

    标签: php wordpress thumbnails


    【解决方案1】:

    试试这个代码。我修改了这些'id'=&gt;$query-&gt;post-&gt;ID&lt;?php get_the_post_thumbnail($item['id']); ?&gt; 代码。

    $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 }?> 
    

    【讨论】:

    • 哦,我是如此接近。非常感谢!
    • 很高兴为您提供帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2019-03-22
    • 2014-06-16
    • 1970-01-01
    • 2022-07-27
    相关资源
    最近更新 更多