1

私は次から次へと Web サイトに取り組み始めましたが、人気のあるウィジェットを作成して、カテゴリから過去 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 に答える