Top 10 Polylang solution
-
So you are trying to use Top 10 plugin with Polylang?
Simple solution:to you function.php file add:
/** * Top 10 most popular posts on wordpress plugin doesn't have Polylang plugin support, * so we have to filter posts on our own side before printing them out * @param $posts - current top 10 plugins posts * @return array - filtered array per language */ function tptn_filter_posts_per_language($posts){ global $polylang; $_posts = []; $limitPostsPerLanguage = 4; // show only 4 posts on home $currentLanguage = pll_current_language('slug'); foreach($posts as $post) { $lang = $polylang->model->get_post_language($post['ID'])->slug; if ($currentLanguage == $lang) { $_posts[] = $post; } if (count($_posts) >= $limitPostsPerLanguage) { break; } } return $_posts; } add_filter('tptn_pop_posts_array','tptn_filter_posts_per_language');
That will filter all top10 posts to just show those which are in current language.
Then on place you want to print them:
<?php if (function_exists('tptn_pop_posts')): ?> <?php // what? limit 100 when we are showing 4?? // oh yes, indeed. it's because we are filtering all posts by languages later. // so there is possibility that with limit 10 there will be 9posts from same language. // setting it to 100 we are assuming that there will be at least 4 posts from different language $_top10posts = tptn_pop_posts(['posts_only' => true, 'limit' => 100]); foreach($_top10posts as $_post): ?> <a href="<?php echo get_permalink($_post['ID']) ?>" title="<?php echo get_the_title($_post['ID']) ?>"><?php echo substr(get_the_title($_post['ID']), 0, 29) ?></a> <?php endforeach; ?> <?php endif; ?>
I hope it will help someone.
Viewing 13 replies - 1 through 13 (of 13 total)
Viewing 13 replies - 1 through 13 (of 13 total)
- The topic ‘Top 10 Polylang solution’ is closed to new replies.