0

在我的 wordpress 主题中,我想在页脚中添加一个侧循环以获取最新帖子。此循环的第一个帖子显示拇指像素、标题和帖子预览。以下 5 个仅显示标题/链接。

由于我已经在主 div 中使用了常规<?php if(have_posts()) : while(have_posts()) : the_post(); ?> ,因此我必须使用基于 get_posts() 的侧循环

这是我想要得到的,但它不起作用:

<?php query_posts('cat=6&showposts=5'); ?>
<?php $posts = get_posts('category=6&numberposts=5'); 
$count = count($posts);
foreach ($posts as $post) : start_wp(); ?>
    <?php if ($count < 2) : ?>

        /// code for the 1st post (thumb etc..)

    <?php else : ?>

           /// code for the 4 following post (links to posts only)          

    <?php endif; ?>
<?php endforeach; ?>

我知道如何为常规 wordpress 循环添加计数/条件,但不使用 get_posts() 函数。

你能帮我实现吗?

提前致谢 ;)


编辑:解决方案:

好的,我使用了 'offset' 参数来实现这一点:

<?php $posts = get_posts('numberposts=2&offset=0'); 
foreach ($posts as $post) : start_wp(); ?>

<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" class="footernews-thumb">
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail(thumbnail); ?>
<?php endif; ?>
</a>

<h2 class="footernews-title"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<p class="footernews-preview"><?php the_content_rss('', TRUE, '', 20); ?></p>

<?php endforeach; ?>        

<?php $posts = get_posts('numberposts=3&offset=1'); 
foreach ($posts as $post) : start_wp(); ?>                      
<h2 class="footernews-title"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>                        
<?php endforeach; ?>

由于“循环”并不是真正的循环,我决定避免计算发生次数。

4

2 回答 2

0

您正在设置$count您获得的帖子数量(5),因此它永远不会少于 2。

您需要将其更改为:

<?php query_posts('cat=6&showposts=5'); ?>
<?php $posts = get_posts('category=6&numberposts=5'); 
$first = true;
foreach ($posts as $post) : start_wp(); ?>
    <?php if ($first) : ?>

        /// code for the 1st post (thumb etc..)
        <?php $first = false; ?>
    <?php else : ?>

           /// code for the 4 following post (links to posts only)          

    <?php endif;?>

<?php endforeach; ?>
于 2013-02-20T22:11:08.687 回答
0

我会像这样使用简单的递增检查....

<?php query_posts('cat=6&showposts=5'); ?>
<?php $posts = get_posts('category=6&numberposts=5'); 
$i = 1;
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 1) : ?>

    /// code for the 1st post (thumb etc..)

<?php else : ?>

       /// code for the 4 following post (links to posts only)          

<?php endif; ?>
<?php ++$i;
<?php endforeach; ?>
<?php unset($i); ?>

这只会为您提供第一次迭代的额外输出,之后它将继续使用较少的代码。

于 2013-02-20T22:22:45.900 回答