在 WordPress 中选择帖子

发布于 2024-11-03 07:47:46 字数 84 浏览 0 评论 0原文

我是 wordress 和 php 的新手。 我需要显示从 2009 年 6 月到 2010 年 6 月的所有帖子。 如何通过创建自定义循环来做到这一点?

I'm new to wordress and php.
I need to display all posts from june 2009 to june 2010.
How can I do that by creating a custom loop?

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

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

发布评论

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

评论(2

花开柳相依 2024-11-10 07:47:46

query_posts() 只允许显示特定周或月的帖子。但是,您可以通过添加几行代码来显示两个日期之间的帖子。您需要将此代码粘贴到主题中您想要显示的任何位置。

<?php
      function filter_where($where = '') {
            $where .= " AND post_date >= '2009-06-01' AND post_date <= '2010-06-30'";
        return $where;
      }
        add_filter('posts_where', 'filter_where');
        query_posts($query_string);
        while (have_posts()) :
          the_post();
          the_content();
        endwhile;
    ?>

来源:http://bit.ly/i5zXP0

query_posts() just allow to show posts from a specific week or month. However, you can show posts between two dates, adding a few lines of code. You need to paste this code wherever in your theme you'd like to display.

<?php
      function filter_where($where = '') {
            $where .= " AND post_date >= '2009-06-01' AND post_date <= '2010-06-30'";
        return $where;
      }
        add_filter('posts_where', 'filter_where');
        query_posts($query_string);
        while (have_posts()) :
          the_post();
          the_content();
        endwhile;
    ?>

Source: http://bit.ly/i5zXP0

隔纱相望 2024-11-10 07:47:46

使用 WP 查询
时间参数

年 (int) - 4 位数年份(例如 2011)。
Monthnum (int) - 月份编号(从 1 到 12)。
w (int) - 一年中的第几周(从 0 到 53)。使用 MySQL WEEK 命令。该模式取决于“start_of_week”选项。
day (int) - 一个月中的第几天(从 1 到 31)。
hour (int) - 小时(从 0 到 23)。
分钟 (int) - 分钟(从 0 到 60)。
第二个 (int) - 第二个(0 到 60)。
m(int)- 年月(例如:201307)。

仅返回当前日期的帖子:

   $today = getdate();
   $query = new WP_Query( 'year=' . $today["year"] . '&monthnum=' . $today["mon"] . '&day=' . $today["mday"] );

仅返回本周的帖子

   $week = date('W');
    $year = date('Y');
    $query = new WP_Query( 'year=' . $year . '&w=' . $week );

返回 2010 年 3 月 1 日至 3 月 15 日的帖子:

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts for March 1 to March 15, 2010
$where .= " AND post_date >= '2010-03-01' AND post_date < '2010-03-16'";
return $where;
 }

 add_filter( 'posts_where', 'filter_where' );
 $query = new WP_Query( $query_string );
 remove_filter( 'posts_where', 'filter_where' );

返回 30 至 60 天的帖子旧

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts  30 to 60 days old
$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . "    AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
 }

 add_filter( 'posts_where', 'filter_where' );
 $query = new WP_Query( $query_string );
 remove_filter( 'posts_where', 'filter_where' );

更多参考请点击这里 http://codex.wordpress.org/Class_Reference/WP_Query

Using WP Query
Time Parameters

year (int) - 4 digit year (e.g. 2011).
monthnum (int) - Month number (from 1 to 12).
w (int) - Week of the year (from 0 to 53). Uses the MySQL WEEK command. The mode is dependent on the "start_of_week" option.
day (int) - Day of the month (from 1 to 31).
hour (int) - Hour (from 0 to 23).
minute (int) - Minute (from 0 to 60).
second (int) - Second (0 to 60).
m (int) - YearMonth (For e.g.: 201307).

Returns posts for just the current date:

   $today = getdate();
   $query = new WP_Query( 'year=' . $today["year"] . '&monthnum=' . $today["mon"] . '&day=' . $today["mday"] );

Returns posts for just the current week:

   $week = date('W');
    $year = date('Y');
    $query = new WP_Query( 'year=' . $year . '&w=' . $week );

Return posts for March 1 to March 15, 2010:

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts for March 1 to March 15, 2010
$where .= " AND post_date >= '2010-03-01' AND post_date < '2010-03-16'";
return $where;
 }

 add_filter( 'posts_where', 'filter_where' );
 $query = new WP_Query( $query_string );
 remove_filter( 'posts_where', 'filter_where' );

Return posts 30 to 60 days old

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts  30 to 60 days old
$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . "    AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
 }

 add_filter( 'posts_where', 'filter_where' );
 $query = new WP_Query( $query_string );
 remove_filter( 'posts_where', 'filter_where' );

For more reference click here http://codex.wordpress.org/Class_Reference/WP_Query

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