the_excerpt仅在最后一篇文章中

时间:2015-08-13 07:54:25

标签: php wordpress posts

所以我有问题,我无法解决。我想获得帖子数据,如标题和摘录。但是当我使用我的代码时,它只显示最后一篇文章的摘录,而其他所有文章都有正常的内容。所以我希望所有帖子都有摘录。

<div class="postai">
<div class="postu_pavadinimas"></div>

<ul>
<?php
$args = array( 'posts_per_page' => 6 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) : setup_postdata( $post ); ?>
    <li>
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <?php the_excerpt();?>
    </li>
    <?php endforeach; 
    wp_reset_postdata(); ?>
</div>
</ul>

编辑:确定1观察,当我将最大帖子更改为3时,我根本没有任何摘录,只有正常的内容。当我更改为'posts_per_page'=&gt; 8,第6和第8帖有摘录。我现在有点困惑....

1 个答案:

答案 0 :(得分:1)

如果有效,请尝试使用get_the_excerpt。让我知道

<div class="postai">
<div class="postu_pavadinimas"></div>

<ul>
<?php
$args = array( 'posts_per_page' => 6 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) : setup_postdata( $post ); ?>
    <li>
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
      <?php echo apply_filters( 'the_excerpt', get_the_excerpt() ); ?>
    </li>
    <?php endforeach; 
    wp_reset_postdata(); ?>
</div>
</ul>