WordPress随机帖子(最近3天的帖子)

时间:2017-01-15 作者:ali

我想显示最后3天的帖子随机。如何编辑此代码?

<?php
$randompost = array(
\'numberposts\' => 2,
\'type\' => \'news\',
\'orderby\' => \'rand\',
\'year\' => date( \'Y\' ),
\'week\' => date( \'W\' ),
);
$rand_posts = get_posts( $randompost );
foreach( $rand_posts as $post ) : ?>
<?php endforeach; ?>

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

您可以使用此代码显示最近3天的帖子。祝你好运。。。

$args = array(
    \'post_status\' => \'publish\',
    \'type\' => \'news\',
    \'posts_per_page\' => 2,
    \'order\' => \'rand\',
    \'date_query\' => array(
        array(
            \'after\' => \'3 days ago\'
        )
    )
);

SO网友:Samuel Asor

您可以使用以下代码:

$today = getdate();
$randompost = array(
    \'date_query\' => array(
        array(
            \'before\'     => \'3 days ago\',
            \'after\'    => array(
                \'year\'  => $today[\'year\'],
                \'month\' => $today[\'mon\'],
                \'day\'   => $today[\'mday\'],
            ),
        ),
    ),
    \'numberposts\' => 2,
    \'type\' => \'news\',
    \'orderby\' => \'rand\',
  );

$rand_posts = get_posts( $randompost );
if ( $rand_posts ) {
foreach ( $rand_posts as $post ) : 
    setup_postdata( $post );
    ?>
    <!-- Your Markup Here -->
    <?php
endforeach; 
wp_reset_postdata();
}
?>
查看更多详细信息here

相关推荐

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

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