0

在 Wordpress 中,我想显示 2 个最新帖子以及第一个帖子的帖子缩略图。

我一直在玩下面的代码,但是当我只想为第一个帖子显示图像时,总是会为第一个帖子和第二个帖子显示图像。

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

2 回答 2

1

您有点错过任何可以让您有选择地显示图像的条件。

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

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

假设$posts是一个基于 0 的枚举数组。注意添加$keyforeach, 以及if打印缩略图之前

于 2013-02-18T19:16:43.097 回答
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; ?>
于 2013-02-19T08:56:37.183 回答