如何自定义 wp_get_archives();与 WordPress 档案

发布于 2024-10-03 17:02:49 字数 436 浏览 0 评论 0原文

我想自定义我的档案以按月排序,但如果是在上一年,则将其分组为年度类别。现在我有:

<ul><?php wp_get_archives('type=postbypost&limit=10'); ?></ul>

这显示了最后 10 篇帖子。我知道以下内容将按月份排序:

<ul><?php wp_get_archives('type=monthly'); ?></ul>

但是当月份小于 12 时,如何让它按年份排序?我希望它看起来像下面这样:

  • 2010 年 3 月
  • 2010 年 2 月
  • 2010 年 1 月
  • 2009

提前致谢。

I want to customize my archives to sort by month, but if it was in the previous year to group itself into a yearly category. Right now I have:

<ul><?php wp_get_archives('type=postbypost&limit=10'); ?></ul>

That shows the last 10 posts. I know that the following will sort by month:

<ul><?php wp_get_archives('type=monthly'); ?></ul>

But how do I get it to sort by year when the month is less recent than 12? I want it to look like the following:

  • March 2010
  • February 2010
  • January 2010
  • 2009

Thanks in advance.

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

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

发布评论

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

评论(1

咋地 2024-10-10 17:02:49

我不知道是否有内置方法可以做到这一点,我会使用带有自定义查询的函数,如下所示:

  SELECT COUNT(ID) posts, YEAR(post_date) y, MONTH(post_date) m 
    FROM $wpdb->posts 
   WHERE post_status = 'publish'
GROUP BY y, m
  HAVING y = YEAR(NOW())
UNION
  SELECT COUNT(ID), YEAR(post_date) y, 0
    FROM $wpdb->posts
   WHERE post_status = 'publish'
GROUP BY y
  HAVING y < YEAR(NOW())
ORDER BY y DESC, m DESC;

然后使用链接和所有内容格式化结果。

I don't know if there's a builtin way to do this, I'd use a function with custom query like this:

  SELECT COUNT(ID) posts, YEAR(post_date) y, MONTH(post_date) m 
    FROM $wpdb->posts 
   WHERE post_status = 'publish'
GROUP BY y, m
  HAVING y = YEAR(NOW())
UNION
  SELECT COUNT(ID), YEAR(post_date) y, 0
    FROM $wpdb->posts
   WHERE post_status = 'publish'
GROUP BY y
  HAVING y < YEAR(NOW())
ORDER BY y DESC, m DESC;

then format the results with links and all.

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