Shortcode Showing the Latest Posts Using Timber
If you need a shortcode showing the latest posts, you can use the snippet below. The snippet uses the Timber plugin, that allows the usage of TWIG as template engine:
public function __construct() {
add_shortcode( 'list_newest_snippets', 'wpkb_shortcode_list_newest_posts' );
}
public function wpkb_shortcode_list_newest_snippets() {
// WP_Query arguments
$args = array(
'post_type' => array( 'post' ),
'posts_per_page' => '5', // Number of posts
'order' => 'DESC',
'orderby' => 'date',
);
$context['posts'] = Timber::get_posts( $args );
return Timber::compile( 'twig/list_newest_snippets.twig', $context );
}
The file twig/list_newest_snippets.twig should look like:
<ul>
{% for post in posts %}
<li><a href="{{post.link}}">{{post.title}}</a></li>
{% else %}
<li>Es wurden keine Snippets gefunden.</li>
{% endfor %}
</ul>
When using the WordPress editor, you can now add the shortcode [[list_newest_snippets]] to show the latest posts.