0

I want to execute the following code only on the homepage for the default post loop. I added this to the functions.php, but it doesn't work. It works and adds the according altering classes if I use it without if condition. Not sure what I am missing. Any help appreciated 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' );
    }
0

Even if wrap your filter in a if statement, the function still will be executed. Try this:

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.