【问题标题】:How to limit the wordpress tagcloud by date?如何按日期限制 wordpress tagcloud?
【发布时间】:2011-02-25 20:13:08
【问题描述】:

我一直在寻找很长一段时间,以找到一种按日期限制 wordpress 标签的方法,并按它们在所选时间范围内出现的次数对它们进行排序。但是我比较失败。

我想要实现的目标类似于 Twitter 上的热门话题。但在这种情况下,“趋势标签”。默认情况下,wordpress tagcloud 会显示有史以来最流行的标签。这对我来说毫无意义,因为我想跟踪当前趋势。

理想情况下是这样的:

当今最流行的标签

  • 奥巴马(18 次提及)
  • 纽约(15 次提及)
  • 钢铁侠(11 次提及)
  • 罗宾汉(7 次提及)

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

【问题讨论】:

    标签: wordpress date tags limit


    【解决方案1】:

    你可以通过查询获取标签列表,这样你就不必循环抛出最后一个 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>
    

    【讨论】:

      【解决方案2】:

      好的,所以我认为您可能想要这样做,例如最后 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 );
      

      【讨论】:

      • 考虑到标签的存储方式,这可能是唯一的方法,但随着帖子数量的增加,速度会变得非常慢......
      • 谢谢!确保以分号结束“$posttags = get_the_tags()”行。我得到了这个与 Executable PHP 小部件一起工作的东西,并用 包围了整个东西。示例:priestessastrology.com
      • @Zade 我添加了分号。一旦您有足够的业力获得权限,请随意在 Stack Overflow 上编辑带有语法错误的答案!
      【解决方案3】:

      我想你可以看看一些插件,看看你是否有你需要的插件

      【讨论】:

        【解决方案4】:

        我很确定标签没有时间戳 - 也许您可以搜索特定时间段内具有特定标签的帖子?

        【讨论】:

        • 他们确实没有时间戳。但是,由于它们与帖子相关,并且帖子确实有时间戳,所以我的想法是应该可以检索这些时间戳。你的回复让我思考。将时间戳表添加到标签不是最简单的吗?
        猜你喜欢
        • 1970-01-01
        • 2018-08-27
        • 2014-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-07
        • 2015-06-06
        • 2012-10-27
        相关资源
        最近更新 更多