【问题标题】:Add Css class to a specific div in PHP Wordpress将 Css 类添加到 PHP Wordpress 中的特定 div
【发布时间】:2015-08-28 12:28:49
【问题描述】:

下面的 php 代码在第二深度时从页面中删除侧边栏,但当此函数删除同一页面上的侧边栏时,我想将以下 div 的类更改为“全角”,因为我的页面有侧栏相同的 css 适用于没有侧边栏且页面为半宽的页面。

    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:

发件人:

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

收件人:

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

【问题讨论】:

    标签: php css wordpress


    【解决方案1】:

    您可以为此使用 js/jquery,它在页面加载后应用。

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

    【讨论】:

    • 上面是一个php代码你确定这个Jquery能用吗??
    • 然后你可以在页面加载后调用 js ......就像下面的代码 $(document).ready(function() { $("#primary").removeClass("content-area") ; $("#primary").addClass("fullwidth"); });
    • 我应该在function.php中添加这个并且有一个问题我想删除这个只有当上面的php删除侧边栏时才应该触发css
    • 不,您可以简单地将脚本链接到您的 HTML &lt;head&gt; 中,例如 &lt;script src="your/path/script.js"&gt;&lt;/script&gt;
    • 如果你想知道你可以添加重新编辑我的问题代码和你的答案我很困惑在哪里添加这个
    【解决方案2】:

    对于你的情况,只需用这个替换你的代码......

    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>';
    
                }
            }
        }
    

    【讨论】:

    • dint 显示相同的工作可能是在加载 html 代码之前加载 php 的功能我不确定
    • 我刚刚添加了一个警报,以检查它是否在 Dom 上工作,准备好警报不显示
    【解决方案3】:

    你需要这样做。

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

    【讨论】:

    • 但是如果我在 if 条件中添加这个,整个网站都会崩溃
    猜你喜欢
    • 2023-02-22
    • 2020-10-23
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多