我一个接一个地开始在一个网站上工作,我不明白如何制作流行的小部件以仅显示来自类别的最近 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;