2010-05-31 77 views
6

我一直在寻找相当长的一段时间来找到一种方法来按日期限制wordpress标签,并根据它们在选定时间范围内出现的次数排序。但我一直都很失败。如何按日期限制wordpress tagcloud?

我试图达到的是类似Twitter上的热门话题。但在这种情况下,“趋势标签”。默认情况下,wordpress tagcloud显示所有时间最受欢迎的标签。这对我来说没有意义,因为我想跟踪当前的趋势。

理想情况下是这样的:

现在最流行的标签

  • 奥巴马(18提及)
  • 纽约(15提及)
  • 铁人(11提及)
  • 罗宾汉(7提到)

然后乘以'本周最受欢迎'和'本月最受欢迎'。有谁知道一种方法来实现这一目标?

回答

0

我敢肯定,标签没有时间戳 - 也许你可以做一个具有特定标签的帖子搜索特定timeperiod?

+0

他们确实没有时间戳。然而,由于它们与帖子相关,并且帖子确实有时间戳,我认为应该可以检索这些时间戳。不过你的回复让我想到了。将标签添加时间戳不是最简单的吗? – Nordin 2010-06-01 13:10:43

3

好的,所以我想你可能想要的是做这个说,最后50个职位。

循环在过去n帖子,提取每个标签的每个帖子的term_id,然后传递串入的wp_tag_cloud()include参数;

$how_many_posts = 50; 
$args = array(
    'posts_per_page' => $how_many_posts, 
    'orderby' => 'date', 
    'order' => 'DESC', 
); 
// get the last $how_many_posts, which we will loop over 
// and gather the tags of 
query_posts($args); 
// 
$temp_ids = array(); 
while (have_posts()) : the_post(); 
    // get tags for each post 
    $posttags = get_the_tags(); 
    if ($posttags) { 
     foreach($posttags as $tag) { 
      // store each tag id value 
      $temp_ids[] = $tag->term_id; 
     } 
    } 
endwhile; 
// we're done with that loop, so we need to reset the query now 
wp_reset_query(); 
$id_string = implode(',', array_unique($temp_ids)); 
// These are the params I use, you'll want to adjust the args 
// to suit the look you want  
$args = array(
    'smallest' => 10, 
    'largest' => 30, 
    'unit'  => 'px', 
    'number' => 150, 
    'format' => 'flat', 
    'separator' => "\n", 
    'orderby' => 'count', 
    'order'  => 'DESC', 
    'include' => $id_string, // only include stored ids 
    'link'  => 'view', 
    'echo'  => true, 

); 
wp_tag_cloud($args); 
+0

可能是这样做的唯一方法,因为标签是如何存储的,但随着帖子数量的增长,它会变得非常慢...... – Kasumi 2010-06-23 09:11:24

+0

谢谢!确保以分号结束“$ posttags = get_the_tags()”行。我使用可执行PHP小部件工作,并使用围绕整个事情。例如:http://www.priestessastrology.com/ – Zade 2012-02-27 23:21:10

+0

@Zade我已经添加了分号。一旦有足够的业力权限,随意编辑堆栈溢出语法错误的答案! – artlung 2012-02-28 01:14:54

0

我觉得你可以看一些插件,看看你有一个像你所需要的插件

0

呦可以得到查询的标签列表,这样你就不必做循环抛出最后一个X帖子。

<ul id="footer-tags"> 
<?php $wpdb->show_errors(); ?> 
<?php 
global $wpdb; 
$term_ids = $wpdb->get_col(" 
    SELECT term_id FROM $wpdb->term_taxonomy 
    INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id=$wpdb->term_relationships.term_taxonomy_id 
    INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id 
    WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= $wpdb->posts.post_date"); 

if(count($term_ids) > 0){ 

    $tags = get_tags(array(
    'orderby' => 'count', 
    'order' => 'DESC', 
    'number' => 28, 
    'include' => $term_ids, 
)); 
foreach ((array) $tags as $tag) { 
echo '<li><a href="' . get_tag_link ($tag->term_id) . '" rel="tag">' . $tag->name . '</a></li>'; 
} 
} 
?> 
</ul>