Home > Net >  Woocommerce , add_action conditionally
Woocommerce , add_action conditionally

Time:08-28

Precisely speaking, I want to change the place of breadcrumbs in category pages. so I check if it is_product_category() and then remove_action ('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 ) and add_action( 'woocommerce_before_shop_loop', 'woocommerce_breadcrumb', 15 );

but apparently it's not the solution, since it doesn't work. Note that it adds the action but doesn't remove it and two breadcrumbs are showing on the pages. Your recommendations are appreciated

CodePudding user response:

You can try this

add_action( 'init', 'my_removal_function');
function my_removal_function() {
remove_action ('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );


}

CodePudding user response:

You need to apply the condition before the first hook priority.

add_action( 'woocommerce_before_main_content', 'adjust_breadcrumb', 10 );

function adjust_breadcrumb() {

   if ( is_product_category() ) {
       remove_action ('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
       add_action( 'woocommerce_before_shop_loop', 'woocommerce_breadcrumb', 15 )
   }
}
  • Related