2013-02-18 69 views
0

在WordPress中,我想显示2个最新帖子以及第一篇文章的帖子缩略图。如何在wordpress中显示最新帖子的图片

我一直在玩下面的代码,但是图像总是以第一篇文章以及第二篇文章的形式显示,当时我只想为第一篇文章展示图片。

<?php 
$cat_args = array(
'orderby' => 'name', 
'order' => 'ASC', 
'child_of' => 0 
); 


$post_args = array(
    'numberposts' => 2, 
    'category' => $category->term_id 
); 

$posts = get_posts($post_args); 

foreach($posts as $post) { 
?> 
    <?php the_title(); ?> 
<?php the_post_thumbnail('blog_post_image'); ?> 
<?php 
} 
} 
?> 

回答

1

你有点失踪,可以让你选择性地显示图像的任何条件。

<?php 
foreach($posts as $key=>$post) { 
    the_title(); 

    if (0 == $key) { 
     the_post_thumbnail('blog_post_image'); 
    } 
} 

假设$posts是一个基于0的枚举数组。打印缩略图

+0

我不是很好用PHP,我哪里会在上面的代码插槽此之前,请注意添加$keyforeach,还有if? – 2013-02-18 19:20:17

+0

@AdamScott替换你现有的'foreach'循环 – 2013-02-18 21:02:35

0
<?php $loop = 1; ?> 
<?php foreach($posts as $post): ?> 
    <?php the_title(); ?> 
    <?php if($loop == 1): ?> 
     <?php the_post_thumbnail('blog_post_image'); ?> 
    <?php endif; ?> 
<?php $loop++; endforeach; ?>