Search code examples
phpwordpressposts

the_excerpt only on last post


So I have problem, which I couldn't solve. I want to get post data like, title and excerpt. But when I use my code it only shows excerpt on the last post and all the others have normal content. So I would like all posts to have 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 the_excerpt();?>
    </li>
    <?php endforeach; 
    wp_reset_postdata(); ?>
</div>
</ul>

EDIT: Ok 1 observation, when i change max posts to 3, i dont get any excerpts at all, only normal contents. When i change to 'posts_per_page' => 8, 6th and 8th posts had excerpts. I am kinda confused right now....


Solution

  • try to use get_the_excerpt if it works . let me know

    <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>