0

Below php code removes the sidebar From the page when that is on 2nd depth but on same when this function removes the sidebar on same page i want to change the class of the below div to "Fullwidth" as my page has side bar the same css is applicable on page where sidebar is not present and the page is of half width.

    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( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
                // could be theme specific ex: Storefront
                remove_action( 'storefront_sidebar', 'storefront_get_sidebar', 10 );




            }
        }
    }
add_action( 'woocommerce_before_main_content', 'so_32165017_conditionally_remove_sidebar' );

HTML:

From:

<div id="primary" class="content-area">

To:

<div id="primary" class="fullwidth">

3 Answers 3

1

You can use js/jquery for this, which is applied after the page loads.

$("#primary").removeClass("content-area");

$("#primary").addClass("fullwidth");
Sign up to request clarification or add additional context in comments.

10 Comments

the above is a php code are you sure this Jquery is gonna work ??
then u can call js after page load.... like below code $(document).ready(function() { $("#primary").removeClass("content-area"); $("#primary").addClass("fullwidth"); });
should i have to add this in a function.php and a have a question i want to remove this only when the above php removes the sidebar the css should be triggered
Nope, you can simply link the script in your HTML <head> like <script src="your/path/script.js"></script>.
if u want mind can you add re edit my question code with your answer am confused where to add this
|
0

For your case just replace your code with this one...

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( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
                // could be theme specific ex: Storefront
                remove_action( 'storefront_sidebar', 'storefront_get_sidebar', 10 );


                echo '<script type="text/javascript">
                    $(document).ready(function() {
                        $("#primary").removeClass("content-area"); 
                        $("#primary").addClass("fullwidth"); 
                    });
               </script>';

            }
        }
    }

2 Comments

dint work showing the same ma be the function of php is loading before the html code is loaded am not sure
i just added a alert to check whether is it working on Dom ready the alert don't show up
0

you need to do this.

 $('#testID2').addClass('fullwidth').removeClass('content-area');

1 Comment

but where the entire site goes down if i add this in if condition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.