Skip to content

Instantly share code, notes, and snippets.

@n0mad01
Forked from varmais/functions.php
Last active April 5, 2018 15:19
Show Gist options
  • Save n0mad01/70f5cbb4d700833ab518b2bfdba65ab1 to your computer and use it in GitHub Desktop.
Save n0mad01/70f5cbb4d700833ab518b2bfdba65ab1 to your computer and use it in GitHub Desktop.
Fetch all latest Wordpress posts when using Polylang plugin
[latest_posts]
OR
[latest_posts number_of_posts=3]
OR
[latest_posts number_of_posts=3 language=en]
<?php
function get_latest_language_posts ($atts)
{
$attributes = shortcode_atts ([
'number_of_posts' => 3,
'language' => pll_current_language()
], $atts);
$posts = get_posts ([
'post_type' => 'post',
'lang' => $attributes['language'],
'showposts' => $attributes['number_of_posts']
]);
foreach ($posts as $article) :
$image = get_the_post_thumbnail($article->ID);
set_query_var ('article', $article);
set_query_var ('image', $image);
get_template_part ('posts-preview');
endforeach;
wp_reset_postdata();
return '';
}
add_shortcode('latest_posts', 'get_latest_language_posts');
<a href="<?php echo get_permalink($article); ?>"><?php echo $article->post_title; ?></a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment