Woocommerce , add_action conditionally

266 Views Asked by At

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

2

There are 2 best solutions below

2
tiny On
You can try this



    
  add_action( 'init', 'my_removal_function');
        function my_removal_function() {
        if ( is_product_category() ) {
        remove_action ('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
        add_action( 'woocommerce_before_shop_loop', 'woocommerce_breadcrumb', 15 )
    }
}
4
Ash0ur On

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 )
   }
}