可能还有另一种方法,但我想理解为什么这段代码返回null。。。
我只有10月份的帖子,下面是递归函数:
function last_post( $month,$year ) {
$args = array( \'monthnum\' => $month, \'year\' => $year );
$query = new WP_Query( $args );
var_dump( $month ); // int 11, int 10
if( $query->have_posts() ){
return $args;
} else {
last_post( $month-1, $year ); // here is the problem ?
}
}
$args = last_post( 11, $year ); // null but ok with 10
SO网友:benoît
只需添加return
函数之前。
function last_post( $month,$year ) {
$args = array( \'monthnum\' => $month, \'year\' => $year );
$query = new WP_Query( $args );
var_dump( $month ); // int 11, int 10
if( $query->have_posts() ){
return $args;
} else {
return last_post( $month-1, $year ); // here is the fix!
}
}
$args = last_post( 11, $year ); // null but ok with 10