问题描述
我想对位于我父主题中的 PHP 文件进行简单修改:
I want to make a simple modification in a PHP file located here in my parent theme:
wp-content hemessailingincwidgetsgallery plase.php
所以我在我的子主题中创建了相同的文件夹结构,并在这个文件中进行了我需要的修改.我还复制/粘贴了声明此小部件所需的所有 PHP 文件.
So I created the same folder structure in my child theme and did the modification I need in this file. I also copied/pasted all the PHP files needed to declare this widget.
wp-content hemessailingincwidgetswidgets.php
wp-content hemessailingincwidgetsgallerygallery.php
wp-content hemessailing-childincwidgetswidgets.php
wp-content hemessailing-childincwidgetsgallerygallery.php
我在这里错过了什么?
推荐答案
WordPress 子主题无法以这种方式工作.您可以通过在子主题中使用相同路径覆盖的唯一文件是基本"文件:index.php、page.php、style.css...主要是模板文件.
WordPress child themes are not working this way. The only files that you can override by using the same path in your child themes are the "basic" files: index.php, page.php, style.css... Mostly the template files.
涉及覆盖子主题中的函数或类.你有几种方法来处理它:
When it comes to overriding functions or classes in a child theme. You've several ways to handle it:
- 重新声明函数/类
- 复制函数/类
- re-declaring the functions/classes
- duplicating the functions/classes
但这取决于您的主题是如何构建的,以及它是否已准备好子主题".让我们看看您的小部件问题.
But it depends on how your theme is built and if it's "child theme" ready. Let's have a look with your widget issue.
如果您在父主题中打开小部件声明文件,您会看到如下内容:
If you open your widget declaration file within your parent theme, you'll see something like:
class Widget_Name extends WP_Widget {
...
CODE OF THE WIDGET
...
参见:https://codex.wordpress.org/Widgets_API
理想的情况是你没有首先看到上面的行但是:
The ideal case is you don't see the above lines first but:
if(!class_exists('Widget_Name')) {
class Widget_Name extends WP_Widget {
...
CODE OF THE WIDGET
...
这意味着,您可以复制/粘贴您的文件,这样就可以正常工作,您的小部件将覆盖父小部件,并且不会抛出任何错误,因为父小部件不会被执行.这就是准备好儿童主题"的主题.请注意,这与函数相同 (if(!function_exists('function_name')
).
Which means, you can just copy/past your file and that'll work just fine, you widget will override the parent one and no error will be thrown as the parent widget won't be executed. That's the "child theme ready" theme. Note that it's the same with functions (if(!function_exists('function_name')
).
不要忘记从您的 child-theme/functions.php
文件中调用您的文件,因为默认情况下不会调用它.
Don't forget to call your file from your child-theme/functions.php
file as it won't be called by default.
喜欢:
require_once('path/to/your/widget_class.php');
其他方式,如果您没有 class_exists 调用只是复制文件,请使用 require_once 调用它.当您定义 2 次相同的类时,您应该会看到一个错误.PHP 不会让这种情况发生,致命错误.
Other way, if you don't have a class_exists call is to just duplicate the file, call it with the require_once. You should see an error as you're defining 2 times the same class. PHP won't let that happen, fatal error.
只需重命名:
class Widget_Name2 extends WP_Widget {
在文件的某处(大部分时间在末尾),查找 register_widget(
并编辑类名:
And somewhere (most of the time at the end) of your file, look for register_widget(
and edit the class name:
register_widget( 'Widget_Name2' );
这不是最方便的方法,因为您将拥有 2 次相同的小部件,但这确实有效.
That's not the most handy way as you'll have 2 times the same widget but that does work though.
这篇关于如何覆盖 WordPress 子主题中的小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!