我尝试使用来自的解决方案显示按类别分组的自定义帖子列表here (第一个选项/示例)。我只对代码进行了一点修改,以满足我的需要,但我的安装/博客(如果没有评论,则会显示白色)会冻结,因为这行代码(在我看来):$term_ids = array_map(function($t) { return $t->term_id, }, $terms);
. 我通过注释部分代码发现了这一点。它怎么了?
我的完整代码:
function yesterday_events2() {
$terms = get_terms( \'event-categories\' );
$term_ids = array_map( function( $t ) { return $t->term_id }, $terms );
$posts = get_posts(
array(
\'nopaging\' => true,
\'tax_query\' => array(
array(
\'taxonomy\' => \'event-categories\',
\'field\' => \'id\',
\'terms\' => $term_ids,
) ),
) );
foreach( $terms as $t ) {
$posts_in_term = array_filter( $posts, function( $p ) use ( $t ) {
// has_term likely triggers a DB hit...
return has_term( $t->term_id, \'event-categories\', $p );
} );
// do stuff with $posts_in_term
}
}