Я пытаюсь добавить класс тела только в 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;
}
Ваш код приходит в 'if (so_32165017_get_product_cat_depth ($ t_id) <2) {' это условие? добавить 'die ('call')', чтобы проверить, удовлетворено ли ваше состояние? – Noman
@Noman, вы тоже можете добавить так: if (so_32165017_get_product_cat_depth ($ t_id) <2) { die ('call') } ' –
весь сайт понижен сверху –