Skip to content

Instantly share code, notes, and snippets.

@audrasjb
Created October 19, 2021 22:36
Show Gist options
  • Save audrasjb/6dc79fd89c834c7a39a792f38a9524e4 to your computer and use it in GitHub Desktop.
Save audrasjb/6dc79fd89c834c7a39a792f38a9524e4 to your computer and use it in GitHub Desktop.
[polylang] create a shortcode to get the 3 last post, of all available languages
function jba_register_shortcode() {
add_shortcode( 'jba_posts', 'jba_shortcode_cb' );
}
add_action( 'init', 'jba_register_shortcode' );
function jba_shortcode_cb( $args ) {
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'tax_query' => '',
'lang' => '',
);
$last_posts = new WP_Query( $args );
$out = '';
if ( $last_posts->have_posts() ) {
$out .= '<ul>';
while ( $last_posts->have_posts() ) {
$last_posts->the_post();
$out .= sprintf(
'<li><a href="%s">%s</a></li>',
get_permalink(),
get_the_title()
);
}
$out .= '</ul>';
}
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment