2010-05-31 39 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>