我开始在另一个开发者之后在一个网站上工作,我不明白如何使热门小部件仅显示过去3个月的类别(类别新闻1个月)的帖子。 我看到有应该这样做的switch语句,但它没有完成它的工作。如何正确地设置间隔?
function getSomePost($category,$postsPerPage=3) {
global $post;
$args = array(
'category_name'=>$category,
'posts_per_page'=>$postsPerPage,
'post_type'=>'post',
'post_status'=>'publish',
'post__not_in'=>array($post->ID),
'orderby'=>'rand',
);
switch ($category)
{
case 'news':
$args['interval'] = '1 MONTH';
break;
case 'analysis':
$args['interval'] = '3 MONTH';
break;
case 'reports':
$args['interval'] = '3 MONTH';
break;
}
$query = new WP_Query($args);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$postThumbClass = 'no-thumb';
?>
<div <?php post_class(array('wp-post-entry', 'sidebar-post' )); ?>>
<?php if(has_post_thumbnail ()):?>
<?php $postThumbClass = '' ?>
<div class="wp-post-thumbnail">
<a href="<?php the_permalink() ?>">
<?php the_post_thumbnail(array(70,70)); ?>
</a>
</div>
<?php endif; ?>
<div class="wp-post-full-content <?php echo $postThumbClass ?> ">
<h3 class="wp-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<!--
<div class="post-content">
<?php the_excerpt() ?>
</div>
//-->
</div>
</div>
}
}
}
更新
我正在尝试更换内部开关
case 'news':
$args['interval'] = '1 MONTH';
break;
带
case 'news':
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string ); //I tried $args and everything
remove_filter( 'posts_where', 'filter_where' );
break;
和
case 'analysis':
$args['interval'] = '3 MONTH';
break;
带
case 'analysis':
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-90 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
break;
答案 0 :(得分:2)
您可以在Time Parameter
中使用$args
来查询特定时段。请参阅Time Parameter文档。
示例:
// Returns posts for just the current week
$week = date('W');
$year = date('Y');
$query = new WP_Query( 'year=' . $year . '&w=' . $week );