通过 functions.php 覆盖小部件
Overriding widget via functions.php
Woocommerce 附带一个默认的产品过滤器小部件,位于
/wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-layered-nav-filters.php
我有很多 products/categories 等等,使用它看起来很乱。我试图整理它并将其变成一个手风琴菜单,从底部的代码中可以看出它似乎可以工作。
但是,我正在努力将它添加到我的主题中。
我使用以下方法创建了自己的小部件:
<?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 中的新代码似乎出错:
Fatal error: Class 'Custom_WC_Widget_Layered_Nav' not found in /homepages/8/d692312546/htdocs/Mytheme/wp-includes/class-wp-widget-factory.php on line 106
我可以做些什么来避免这个错误吗?
正如您在下面看到的,编码似乎有效并且产生了预期的效果。
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: '795'; /* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;
}
h1.widget-title.active:after {
content: "96"; /* 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>
据此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.
}
Woocommerce 附带一个默认的产品过滤器小部件,位于
/wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-layered-nav-filters.php
我有很多 products/categories 等等,使用它看起来很乱。我试图整理它并将其变成一个手风琴菜单,从底部的代码中可以看出它似乎可以工作。
但是,我正在努力将它添加到我的主题中。
我使用以下方法创建了自己的小部件:
<?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 中的新代码似乎出错:
Fatal error: Class 'Custom_WC_Widget_Layered_Nav' not found in /homepages/8/d692312546/htdocs/Mytheme/wp-includes/class-wp-widget-factory.php on line 106
我可以做些什么来避免这个错误吗?
正如您在下面看到的,编码似乎有效并且产生了预期的效果。
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: '795'; /* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;
}
h1.widget-title.active:after {
content: "96"; /* 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>
据此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.
}