1

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

1 回答 1

2

您可以Time Parameter在您$args的查询中使用特定时间段。请参阅时间参数文档。

例子:

// Returns posts for just the current week
$week = date('W');
$year = date('Y');
$query = new WP_Query( 'year=' . $year . '&w=' . $week );
于 2013-05-19T18:11:02.023 回答