Question

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' );
    }
Was it helpful?

Solution

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' );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top