我只想在主页上为默认的post循环执行以下代码。我将此添加到函数中。php,但它不起作用。如果我在没有if条件的情况下使用它,它会工作并添加相应的修改类。不知道我错过了什么。感谢您的帮助Thx,C
global $current_class;
$current_class = \'flex-container\';
function rt_oddeven_post_class ( $classes ) {
global $current_class;
$classes[] = $current_class;
$current_class = ($current_class == \'flex-container\') ? \'flex-container-reverse\' : \'flex-container\';
return $classes;
}
if ( is_home() || is_front_page() ) {
add_filter ( \'post_class\' , \'rt_oddeven_post_class\' );
}
最合适的回答,由SO网友:Cesar Henrique Damascena 整理而成
即使将过滤器包装在if
声明function
仍将执行。尝试以下操作:
global $current_class;
$current_class = \'flex-container\';
function rt_oddeven_post_class ( $classes ) {
if ( is_home() || is_front_page() ) {
global $current_class;
$classes[] = $current_class;
$current_class = ($current_class == \'flex-container\') ? \'flex-container-reverse\' : \'flex-container\';
return $classes;
}
}
add_filter ( \'post_class\' , \'rt_oddeven_post_class\' );