在一个特定于类别的模板上,我想列出过去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,
);
最合适的回答,由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>\';
}