0

我想展示来自“精选”类别的 10 篇文章。只有第一个应该有标题和摘录,其余 9 个只有标题。我是怎么做到的?

非常感谢您的帮助。

4

1 回答 1

2

当您循环浏览帖子时,只需检查$post循环中的当前是否是函数返回中的第一个get_posts

<?php
    $featured = get_posts('numberposts=10&category_name=featured'); // Your original query

    foreach($featured as $post):
        $first = ($post == $featured[0]);
        setup_postdata( $post );

        if($first): ?>

        <div class="post main-featured">
            <h2><?php the_title(); ?></h2>
            <p><?php the_excerpt(); ?></p>
        </div>

        <?php else: ?>

        <div class="post featured">
            <h2><?php the_title(); ?></h2>
        </div>

        <?php endif;        
    endforeach;
?>
于 2009-11-28T16:15:44.573 回答