【问题标题】:Overriding widget via functions.php通过functions.php覆盖小部件
【发布时间】:2017-10-07 16:48:18
【问题描述】:

Woocommerce 带有一个默认的产品过滤器小部件,位于

/wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-layered-nav-filters.php

我有很多产品/类别等,使用它看起来很乱。我试图整理它并将其变成一个手风琴菜单,从底部的代码中可以看出它似乎可以工作。

但是,我正在努力将其添加到我的主题中。

我使用以下方法创建了自己的小部件:

<?php>
    class Custom_WC_Widget_Layered_Nav extends WC_Widget_Layered_Nav {
        Public function widget( $args, $instance ) {
          //taken details prom default plugin and inserted here
		}
    ?>	

    <script>
        var acc = document.getElementsByClassName("widget-title");
        var i;

        for (i = 0; i < acc.length; i++) {
            acc[i].onclick = function() {
                this.classList.toggle("active");
                var panel = this.nextElementSibling;
                if (panel.style.maxHeight){
                panel.style.maxHeight = null;
                } else {
                panel.style.maxHeight = panel.scrollHeight + "px";
                } 
            }
        }
    </script>

我已将其保存在:

/wp-content/themes/my-theme/woocommerce/includes/widgets/custom-wc-widget-layered-nav.php

然后我将以下代码添加到functions.php中:

add_action( 'widgets_init', 'err_override_woocommerce_widgets', 15 );
function err_override_woocommerce_widgets() {
    if ( class_exists( 'WC_Widget_Layered_Nav' ) ) {
        unregister_widget( 'WC_Widget_Layered_Nav' );
        include_once( 'wp-content/themes/my-theme/woocommerce/includes/widgets/custom-wc-widget-layered-nav.php' );
        register_widget( 'Custom_WC_Widget_Layered_Nav' );
    }
} 

functions.php 中的新代码似乎报错:

致命错误:在第 106 行的 /homepages/8/d692312546/htdocs/Mytheme/wp-includes/class-wp-widget-factory.php 中找不到类“Custom_WC_Widget_Layered_Nav”

我有什么可以改变的来避免这个错误吗?

正如您在下面看到的那样,编码似乎有效并且产生了预期的效果。

var acc = document.getElementsByClassName("widget-title");
    var i;

    for (i = 0; i < acc.length; i++) {
        acc[i].onclick = function() {
            this.classList.toggle("active");
            var panel = this.nextElementSibling;
            if (panel.style.maxHeight){
            panel.style.maxHeight = null;
            } else {
            panel.style.maxHeight = panel.scrollHeight + "px";
            } 
        }
    }
h1.widget-title {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 5px;
    width: 100%;
    text-align: left;
    border: none;
    outline: none;
    transition: 0.4s;
    margin-top:5px;
}

/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
h1.widget-title.active, h1.widget-title:hover {
    background-color: #ccc;
}

/* Style the accordion panel. Note: hidden by default */
aside>ul {
    padding: 0 18px;
    background-color: white;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.2s ease-out;
}
h1.widget-title:after {
    content: '\02795'; /* Unicode character for "plus" sign (+) */
    font-size: 13px;
    color: #777;
    float: right;
    margin-left: 5px;
}

h1.widget-title.active:after {
    content: "\2796"; /* Unicode character for "minus" sign (-) */
}

#secondary .widget {
    font-size: 0.86em;
}
.widget-area > aside {
    background: white;
    border: 1px solid #ededed;
    border-radius: 6px;
    padding: 5px 20px;
    margin-bottom: 5px;
    width:20em
}
.widget-title {
    font-size: 1.43em;
}
<div class="widget-area" id="secondary" role="complementary">
    <aside class="widget woocommerce widget_layered_nav" id="Woocommerce-layered-nav-1">
        <h1 class="widget-title">Title1</h1>
        <ul>
            <li class="layered-nav-term">
                <a href="http....co.uk">nav term</a>
                <span class="count">(10)</span>
            </li>
        </ul>
    </aside>
    <aside class="widget woocommerce widget_layered_nav" id="Woocommerce-layered-nav-2">
        <h1 class="widget-title">Title2</h1>
        <ul>
            <li class="layered-nav-term">
                <a href="http....co.uk">nav term2</a>
                <span class="count">(10)</span>
            </li>
        </ul>
    </aside>
</div>

【问题讨论】:

    标签: php wordpress woocommerce widget


    【解决方案1】:

    据此article

    您必须根据保存此小部件的位置更改路径:

    add_action( 'widgets_init', 'err_override_woocommerce_widgets', 15 );
    
    function err_override_woocommerce_widgets() {
      // Ensure our parent class exists to avoid fatal error (thanks Wilgert!)
    
      if ( class_exists( 'WC_Widget_Layered_Nav' ) ) {
    
            unregister_widget( 'WC_Widget_Layered_Nav' );
    
            include_once( 'woocommerce/includes/widgets/custom-wc-widget-layered-nav.php' );
    
            register_widget( 'Custom_WC_Widget_Layered_Nav' );
      }
    
    }
    

    希望它有效!

    更新:

    想到的是首先将脚本保存在您的 js 文件夹中,并将其命名为 custom.js,然后您必须按如下方式注册该脚本:

    // Register your assets during `wp_enqueue_scripts` hook inside `functions.php`.
    function custom_register_scripts() {
        // Give the path of the script
        wp_register_script('js-custom', 'path/to/js/custom.js',array('jquery'));
    }
    

    然后您可以将您的脚本排入您的小部件所在的位置。

    public function widget( $args, $instance ) {
            // Enqueue needed assets inside the `widget` function.
            wp_enqueue_script('js-custom');
    
            // Output widget contents.
        }
    

    【讨论】:

    • 太棒了,干杯,对我的错误进行了排序,但功能似乎不起作用。我不确定 javascript 是否适用于小部件:-(
    • 确实如此,但您必须小心添加脚本并将其加入队列。看看更新部分,我希望它有效!
    • 感谢您抽出宝贵时间提供帮助。我是个新手,哈哈。我已尝试采用您在上述更新中所说的内容,但仍未使新 Javascript 的功能生效。我知道我可能把它放在错误的地方或犯了一个简单的错误,但我没有经验或知识来知道我哪里出错了。
    • Enigma,再次感谢您的帮助。我发现我犯了一个简单的错误,我错过了:add_action('wp_enqueue_scripts','nav_scripts'); 以上function custom_register_scripts() { wp_register_script('js-custom', 'path/to/js/custom.js',array('jquery'));}
    猜你喜欢
    • 1970-01-01
    • 2010-12-09
    • 2012-04-10
    • 2019-08-22
    • 2017-10-31
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    相关资源
    最近更新 更多