![WordPress development tutorials](https://j1de48.p3cdn1.secureserver.net/wp-content/uploads/2014/10/wordpress-tutorial-e1517349774253-1000x550.png)
How to Generate a Post Excerpt Outside the Loop
…> <ul> <?php foreach ( $posts as $post ) : ?> <li> <a href=”<?php echo esc_url( get_permalink( $post->ID ) ); ?>”><?php echo esc_html( $post->post_title ); ?></a>: <?php if ( empty( $post->post_excerpt ) ) : ?> <?php echo wp_kses_post( wp_trim_words( $post->post_content, 20 ) ); ?> <?php else : ?> <?php echo wp_kses_post( $post->post_excerpt ); ?> <?php endif; ?> </li> <?php endforeach; ?> </ul> Our post lists will show the excerpt if set, or gene…