WordPress — выводим самые популярные теги в последних постах
Задача: вывести последние 7 тегов, которые чаще всего проставлялись в последних 10-и статьях.
Пример когда это может понадобиться: тренды http://hi-news.ru/.
function getLastTags($number = 7, $numberposts = 10) { $output = '<ul class="nav navbar-nav">'; $recent_posts = wp_get_recent_posts(array('post_status' => 'publish', 'numberposts' => $numberposts)); $tagIds = array(); $tags = array(); foreach ($recent_posts as $recent_post) { $postTags = wp_get_post_tags($recent_post['ID']); foreach ($postTags as $tag) { if(!isset($tagIds[$tag->term_id])) { $tagIds[$tag->term_id] = 1; $tags[$tag->term_id] = $tag; } else { $tagIds[$tag->term_id]++; } } } arsort($tagIds); $tagIds = array_slice($tagIds, 0, $number, true); foreach ($tagIds as $tagId => $counter) { $output .= '<li><a href="' . get_tag_link($tags[$tagId]->term_id) . '" rel="tag">' . $tags[$tagId]->name . '</a></li>'; } $output .= '</ul>'; return $output; } |
31 Мар, 2016
seoonly.ru
2 апреля 2016Спасибо. Может когда-нибудь пригодится