-
-
Save n0mad01/70f5cbb4d700833ab518b2bfdba65ab1 to your computer and use it in GitHub Desktop.
Fetch all latest Wordpress posts when using Polylang plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[latest_posts] | |
OR | |
[latest_posts number_of_posts=3] | |
OR | |
[latest_posts number_of_posts=3 language=en] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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