如何按日期限制wordpress tagcloud?

发布于 2024-09-04 06:32:22 字数 373 浏览 2 评论 0 原文

我已经搜索了很长一段时间,以找到一种方法来按日期限制 WordPress 标签,并按它们在选定时间范围内出现的次数对它们进行排序。但我一直比较不成功。

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

理想情况下,它会是这样的:

今天最受欢迎的标签

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

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

I've been searching for quite a while now to find a way to limit wordpress tags by date and order them by the amount of times they appeared in the selected timeframe. But I've been rather unsuccesful.

What I'm trying to achieve is something like the trending topics on Twitter. But in this case, 'trending tags'. By default the wordpress tagcloud displays the most popular tags of all time. Which makes no sense in my case, since I want to track current trends.

Ideally it would be something like:

Most popular tags of today

  • Obama (18 mentions)
  • New York (15 mentions)
  • Iron Man (11 mentions)
  • Robin Hood (7 mentions)

And then multiplied for 'most popular this week' and 'most popular this month'. Does anyone know of a way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

轻拂→两袖风尘 2024-09-11 06:32:22

好的,所以我想您可能想要的是对最后 50 个帖子执行此操作。

循环遍历最后 n 个帖子,提取每个帖子的每个标签的 term_id,然后将该字符串传递到 include 参数中="http://codex.wordpress.org/Template_Tags/wp_tag_cloud" rel="nofollow noreferrer">wp_tag_cloud();

$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 );

Okay, so what I think you probably want is to do this for say, the last 50 posts.

Loop over the last n posts, extract the term_id of each tag for each post, then pass that string into the include argument of wp_tag_cloud();

$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 );
烟酉 2024-09-11 06:32:22

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

I'm pretty sure that Tags does not have timestamps - perhaps you could do a search for posts with specific tags for a certain timeperiod?

北方的巷 2024-09-11 06:32:22

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

I think you can look at some of the plugins and see if your have a plugin like what you need

桃气十足 2024-09-11 06:32:22

您可以通过查询获取标签列表,这样您就不必循环抛出最后一个帖子。

<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>

Yo can get the tag list with a query so you don't have to make a loop throw the last X post.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文