显示最近2天的热门帖子

时间:2017-04-24 作者:Ms Hannibal

我有侧边栏。php,它显示了我的wordpress帖子的10个popularpost。但我只想显示过去两天的popularpost。我如何纠正它?

谢谢

                    </div>
                <?php } if (of_get_option(\'most_radio\')==1) { ?>
                 <div class="rmenu col-lg-12 col-sm-12 col-xs-12">
                    <header class="title-panel">
                        <div class="title-arrow"></div>
                        <h2 class="title-text"><?php echo of_get_option(\'most_title\'); ?></h2>

                    </header>
                    <footer class="most-viewed">
                        <div class="most-viewed-content mCustomScrollbar" data-mcs-theme="dark-thin">

                            <?php if(of_get_option(\'most_radio_select\')==1){
                                    $popularpost = new WP_Query( array(
                                    \'post_status\' =>\'publish\',
                                    \'post_type\' =>\'post\',
                                    \'posts_per_page\' => of_get_option(\'most_count\'),
                                    \'orderby\' => \'meta_value_num\',
                                    \'meta_key\' => \'post_views_count\',
                                    \'order\' => \'DESC\'
                                    )
                                ); ?>
                                <ol>
                            <?php
                                if($popularpost->have_posts()) : while($popularpost->have_posts()) : $popularpost->the_post();
                            ?>

                                    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> - <?php echo getPostViews(get_the_ID()); ?> </li>

                            <?php endwhile; endif; ?></ol> <? }else{?>

                            <ol>
                            <?php
                                $big_query = new WP_Query(array(
                                \'post_status\' =>\'publish\',
                                \'post_type\' =>\'post\',
                                \'cat\' => of_get_option(\'most_select_categories\'),
                                \'posts_per_page\' => of_get_option(\'most_count\'))); ?>
                                <?php if($big_query->have_posts()) : while($big_query->have_posts()) : $big_query->the_post();?>

                                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                                <?php endwhile; ?>
                    <?php endif; ?>

                            </ol>
                            <?php } ?>

2 个回复
最合适的回答,由SO网友:v-stackOverFollower 整理而成

在参数列表中,请同时传递date\\u查询,请尝试以下示例。

$args = array(
    \'posts_per_page\' => 10,
    \'post_type\' => \'post\',
    \'order\' => \'DESC\',
    \'date_query\' => array(
        \'after\'     => \'2 days ago\',  
        \'inclusive\' => true
    )
); 
$posts = get_posts($args);

SO网友:Mervan Agency

我认为您忘记在查询中添加date\\u查询参数,请检查以下示例。

$args = array(
\'posts_per_page\' => of_get_option(\'most_count\'),
\'post_type\' => \'post\',
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'post_views_count\', 
\'order\' => \'DESC\',
\'post_status\' =>\'publish\', 
\'date_query\' => array(
    \'after\' => date(\'Y-m-d\', strtotime(\'-2 days\')) 
));

 $posts = get_posts($args);
从3.7开始,您可以使用date\\u查询http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请