0

在 WordPress 上,我怎样才能完成这项工作?

将 wp 查询限制为仅列出最近一年(365 天)的帖子,并且不列出早于 365 天的帖子。

有这个插件吗?请指教。

4

2 回答 2

3

codex 上有一个示例,显示了如何列出过去 30 天的帖子: http: //codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

您应该调整它以满足您的需求,例如(未测试):

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-1 year')) . "'";
    return $where;
}

add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
于 2012-10-22T07:45:49.307 回答
-1
<?php 

$date1yrback = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " - 365 day")); // Find Date 1 year back

$countp = 0;

if ( have_posts() ) : while ( have_posts() ) : the_post();

$my_date = the_date('Y-m-d', FALSE);    //Find Post Date

if($my_date > $date1yrback)             //Check if Post date is greater than date year back
{
    echo "<h1>".the_title()."</h2>";
    echo "<p>".the_content()."</p>";
    $countp++;                          //Increment counter as post in above criteria is found
}

endwhile; endif; 

if($countp == 0)
{
    ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php 
}

?>
于 2012-10-04T11:39:23.580 回答