我想通过以下方式创建帖子布局:
+--------------------+
| 1 Full Width Post |
+----------+---------+
| 2 | 3 |
+--------------------+
| 4 Full Width Post |
+----------+---------+
| 5 | 6 |
+----------+---------+
所以基本上我想用
post_class()
. 例如,对于全宽,它将是“全宽”类,对于半宽,它将是“半宽”类。
我可以通过以下方法添加偶数/奇数类:
function alternating_post_class( $classes ) {
global $wp_query;
$classes[] = ( $wp_query->current_post % 2 === 0 ? \'odd\' : \'even\' );
return $classes;
}
add_filter(\'post_class\', \'alternating_post_class\');
任何帮助都将不胜感激。
最合适的回答,由SO网友:s_ha_dum 整理而成
只是需要一点更复杂的逻辑:
function alternating_post_class($classes) {
static $counter = 1;
switch ($counter) {
case 1:
$classes[] = \'full-width\';
break;
case 2:
case 3;
$classes[] = \'half-width\';
break;
}
$counter = ($counter == 3) ? 1 : $counter + 1;
return $classes;
}
add_filter(\'post_class\', \'alternating_post_class\');