2015-09-13 5 views
1

Я пытаюсь добавить класс тела только в function so_32165017_conditionally_remove_sidebar, если триггеры условий означают, что когда действие удаления удаляет боковую панель в это время для меня add_filter('body_class', 'my_class_names');, этот фильтр добавления должен быть запущен, но в моем случае, если я добавляю заявление add_filter как независимое, он работает нормально, если я добавлю то же самое в случае, если это не так, где я иду неправильно, не уверен.WordPress trigger action filter in if condition

function so_32165017_get_product_cat_depth($catid) 
{ 
    $depth = 0; 
    while ($catid) { 
     $cat = get_term_by('id', $catid, 'product_cat'); // get the object for the catid 
     if ($cat->parent > 0) { 
      $depth++; 
     } 
     $catid = $cat->parent; // assign parent ID (if exists) to $catid 
// the while loop will continue whilst there is a $catid 
    } 
    return $depth; 
} 


add_action('woocommerce_before_main_content', 'so_32165017_conditionally_remove_sidebar'); 
/** 
* Hide the sidebar for items 2 levels deep or more 
*/ 
function so_32165017_conditionally_remove_sidebar() 
{ 
    if (is_product_category()) { 
     $t_id = get_queried_object()->term_id; 
     if (so_32165017_get_product_cat_depth($t_id) < 2) { 

      remove_action('storefront_sidebar', 'storefront_get_sidebar', 10); 

// ****when added hear it is not working ****??// 
      add_filter('body_class', 'my_class_names'); 
     } 
    } 
} 

Добавить класс Код:

// **** works fine hear ****// 
add_filter('body_class', 'my_class_names'); 

function my_class_names($classes) { 
    // add 'class-name' to the $classes array 
    $classes[] = 'My-Class'; 
    // return the $classes array 
    return $classes; 
} 
+0

Ваш код приходит в 'if (so_32165017_get_product_cat_depth ($ t_id) <2) {' это условие? добавить 'die ('call')', чтобы проверить, удовлетворено ли ваше состояние? – Noman

+0

@Noman, вы тоже можете добавить так: if (so_32165017_get_product_cat_depth ($ t_id) <2) { die ('call') } ' –

+0

весь сайт понижен сверху –

ответ

1

Что вам нужно обратить ваше действие отдельно. ваше действие woocommerce_before_main_content запускается после класса body, поэтому добавление фильтра после действия не будет работать.

add_action('woocommerce_before_main_content', 'so_32165017_conditionally_remove_sidebar'); 
function so_32165017_conditionally_remove_sidebar() 
{ 
    if (is_product_category()) { 
     $t_id = get_queried_object()->term_id; 
     if (so_32165017_get_product_cat_depth($t_id) < 2) { 
      remove_action('storefront_sidebar', 'storefront_get_sidebar', 10); 
     } 
    } 
} 

add_filter('body_class', 'my_class_names',10,2); 
function my_class_names($classes) { 
    if (is_product_category()) { 
     $t_id = get_queried_object()->term_id; 
     if (so_32165017_get_product_cat_depth($t_id) < 2) { 
      $classes[] = 'My-Class'; 
     } 
    } 
    // return the $classes array 
    return $classes; 
} 
0

Это не работает, потому что действие woocommerce_before_main_content срабатывает после того, как body_class() уже назвали, так что вы должны использовать предыдущую.

Или вы могли бы использовать body_class фильтр непосредственно:

add_filter('body_class', 'so_32165017_conditionally_remove_sidebar'); 
/** 
* Hide the sidebar for items 2 levels deep or more 
*/ 
function so_32165017_conditionally_remove_sidebar($classes) 
{ 
    if (is_product_category()) { 
     $t_id = get_queried_object()->term_id; 
     if (so_32165017_get_product_cat_depth($t_id) < 2) { 

      remove_action('storefront_sidebar', 'storefront_get_sidebar', 10); 
      $classes[] = 'my_class_names'; 

     } 
    } 
    return $classes; 
}