列出过去一周的所有帖子,按子类别分组

时间:2014-10-06 作者:draney

在一个特定于类别的模板上,我想列出过去7天中单个类别的所有帖子,并按子类别对它们进行分组。

这是我到目前为止的代码,它按类别列出了帖子,但不限于过去一周:

<?php
$categories =  get_categories( \'child_of=83\' );  
foreach ( $categories as $category ) {
        //Display the sub category information using $category values like $category->cat_name
        echo \'<h3>\' . $category -> name . \'</h3>\';
        echo \'<ul>\';
        foreach ( get_posts( \'cat=\' . $category -> term_id ) as $post ) {
            setup_postdata( $post );
            echo \'<li><a href="\' . get_permalink( $post -> ID ) . \'">\' . get_the_title() . \'</a></li>\';   
        }
        echo \'</ul>\';
    }
wp_reset_postdata(); 
?>

How can I show only published posts dated within the past 7 days, including today?

我想我会尝试使用WP\\u Query date\\u Query参数来限制列表,但我不知道如何做到这一点:

$args = array(
    \'date_query\' => array(
        \'after\' => \'1 week ago\',
        \'before\' => \'tomorrow\',
    ),
    \'nopaging\' => true,
);

1 个回复
最合适的回答,由SO网友:Cyclonecode 整理而成

我建议您使用WP_Query 对于这个:

$categories =  get_categories( \'child_of=83\' );  
foreach ( $categories as $category ) {
    echo \'<h3>\' . $category -> name . \'</h3>\';
    echo \'<ul>\';

    // create a WP_Query that retreives all posts from the specified
    // category which is older then 1 week
    $args = array(
      \'cat\' => $category->cat_ID,
      \'date_query\' => array(
         array(
          \'column\' => \'post_date\',
          \'after\' => \'1 week ago\'
         )
       ),
      \'posts_per_page\' => -1,
      \'post_type\' => \'post\',
      \'post_status\' => \'publish\'
    );
    $query = new WP_Query($args);

    // check so there is some posts in the resultset
    if($query->have_posts()) {
      // loop through the result
      while($query->have_posts()) {
        $query->the_post();
        // output data here
      }
    }

    echo \'</ul>\';
}

结束

相关推荐

来自jQuery AJAX的WP API删除请求

我正在使用rest json API插件http://wp-api.org/ 创建一个小应用程序。我试图用jquery ajax删除一个自定义的帖子条目,但缺少一些东西,下面是我的代码:... //make the request var dataArray = {}; var urlRequest = WP_API_Settings.root+\"/posts/\"+postID; var typeRequest = \'D