三个回路
http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters
$time = time();
foreach ( range( 0, 2 ) as $yesterday ):
// get relative time offset by $yesterday days
$rel_time = strtotime( "-$yesterday days", $time );
// get the digits
$year = date( \'Y\', $rel_time );
$day = date( \'d\', $rel_time );
$month = date( \'m\', $rel_time );
// get the posts
foreach ( get_posts( \'year=\' . $year . \'&monthnum=\' . $month . \'&day=\' . \'$day&posts_per_page=3\' ) as $post ):
setup_postdata( $post );
the_title();
the_content();
endforeach;
endforeach;
您还可以围绕
$time
以其他更有效的方式。记住3个循环=3个数据库查询。
SQL
http://codex.wordpress.org/Class_Reference/wpdb
global $wpdb;
$sql = "SELECT * FROM `$wpdb->posts` WHERE 1=0";
$time = time();
for ( $i = 0; $i < 3; $i++ ) {
$time = strtotime( "-1 day", $time ); // deduct one day every time
$year = $wpdb->escape( date( \'Y\', $time ) );
$day = $wpdb->escape( date( \'d\', $time ) );
$month = $wpdb->escape( date( \'m\', $time ) );
$sql .= " OR `ID` IN ( SELECT `ID` FROM `$wpdb->posts` WHERE YEAR(`post_date`) = \'$year\' AND MONTH(`post_date`) = \'$month\' AND DAY(`post_date`) = \'$day\' LIMIT 3)";
}
foreach( $wpdb->get_results( $sql, OBJECT_K ) as $post ):
setup_postdata( $post );
// ...display stuff
endforeach;
这会导致如下结果:
SELECT *
FROM `wp_posts`
WHERE 1=0
OR `ID` IN (
SELECT `ID`
FROM `wp_posts`
WHERE YEAR(`post_date`) = \'2012\'
AND MONTH(`post_date`) = \'03\'
AND DAY(`post_date`) = \'30\'
LIMIT 3)
OR `ID` IN (
SELECT `ID`
FROM `wp_posts`
WHERE YEAR(`post_date`) = \'2012\'
AND MONTH(`post_date`) = \'03\'
AND DAY(`post_date`) = \'29\'
LIMIT 3)
OR `ID` IN (
SELECT `ID`
FROM `wp_posts`
WHERE YEAR(`post_date`) = \'2012\'
AND MONTH(`post_date`) = \'03\'
AND DAY(`post_date`) = \'28\'
LIMIT 3)
大查询,包含3个子查询。多毛的
转义计数器
$seendays = array(); // keep track of the day numbers
if ( have_posts() ) : while ( have_posts() ) : the_post();
if ( isset( $seendays[get_the_date( \'Y-m-d\' )] ) && $seendays[get_the_date( \'Y-m-d\' )] > 3 ) continue;
$seendays[get_the_date( \'Y-m-d\' )] = isset( $seendays[get_the_date( \'Y-m-d\' )] ) ? $seendays[get_the_date( \'Y-m-d\' )] + 1 : 1;
// ...display stuff
endif;
这三种方法都是非常原始的,需要在这里和那里进行一些微调。希望这有帮助。