如何在WordPress的子主题中覆盖小部件? [英] How to override a widget in a child theme in WordPress?

查看:177
本文介绍了如何在WordPress的子主题中覆盖小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在位于父主题此处的PHP文件中进行简单的修改:

I want to make a simple modification in a PHP file located here in my parent theme:

wp-content\themes\sailing\inc\widgets\gallery\tpl\base.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\themes\sailing\inc\widgets\widgets.php  
wp-content\themes\sailing\inc\widgets\gallery\gallery.php

wp-content\themes\sailing-child\inc\widgets\widgets.php  
wp-content\themes\sailing-child\inc\widgets\gallery\gallery.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:

  1. 重新声明功能/类
  2. 复制功能/类
  1. re-declaring the functions/classes
  2. 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对其进行调用.您在定义两次相同类时会看到错误. 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的子主题中覆盖小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆