Roots Discourse

Excerpt from Gutenberg blocks inside loop

I’m using sage and sage-directives but to make my snippet as simple as possible i’m using raw php to make my issue as clear enough.

I have a simple loop with the 3 latest posts. The posts are using several custom Gutenberg blocks. Below everything works right except the excerpt. It’s using the excerpt from the first post for all posts.

<?php
$args = array(
  'post_type'      => 'post',
  'post_status'    => 'publish',
  'posts_per_page' => 3,
);

$loop = new WP_Query($args);

while ($loop->have_posts()) : $loop->the_post(); ?>
  <div class="item">
    <h2><?php the_title(); ?></h2>
    <?php
    $content = do_blocks(get_the_content());
    $excerpt = wp_trim_words($content, 20);
    echo $excerpt;
    ?>
  </div>
<?php endwhile;

wp_reset_postdata();
?>

I tried several things but i cant work it out. It should be something simple but i cant find it. Anybody?

This topic was automatically closed after 42 days. New replies are no longer allowed.