Skip to content

Instantly share code, notes, and snippets.

@erighetto
Last active January 3, 2016 16:29
Show Gist options
  • Save erighetto/8489336 to your computer and use it in GitHub Desktop.
Save erighetto/8489336 to your computer and use it in GitHub Desktop.
Displaying recent posts in WordPress with WPML plugin
<?php
function icl_get_recent_posts($limit = 10, $language) {
global $wpdb;
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if (is_plugin_active('sitepress-multilingual-cms/sitepress.php')) {
$querystr = "
SELECT wposts.*
FROM {$wpdb->prefix}posts wposts, {$wpdb->prefix}icl_translations icl_translations
WHERE wposts.ID = icl_translations.element_id
AND icl_translations.language_code = '".$language."'
AND icl_translations.element_type = 'post_post'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC LIMIT ".$limit."
";
return $wpdb->get_results($querystr, OBJECT);
}
}
// get 3 recent posts in currently active language
$recent_posts = icl_get_recent_posts(3, ICL_LANGUAGE_CODE);
// Example 1: print only formatted link
foreach ($recent_posts as $post) {
icl_link_to_element($post->ID, 'post', $post->post_title );
}
// Example 2: print title link, and excerpt
foreach($recent_posts as $post) : setup_postdata($post); ?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<div>
<?php $texter = get_the_excerpt();
if(strlen($texter ) > 300) {
$texter = substr($texter , 0, 300);
}
echo ''.$texter.'[...]';
?>
</div>
<?php endforeach; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment