问题描述

我试图覆盖一个主题在 child-theme 中的功能,但我无法让它工作。在 functions.php 的主题我可以看到这个 require_once 行:

require_once("inc/alterna-functions.php");

我想覆盖该文件中的一个函数。我刚将原始代码从 alterna/functions.php 复制到 alterna-child/functions.php,并进行了如下更改:

if (!function_exists('alterna_get_social_list')) :
    function alterna_get_social_list($extra_name = '', $topbar = false, $data = null, $target = '_blank')
    {
        global $alterna_options;

        $str = "";
        $social_list = array(
            array('twitter', 'Twitter'),
            array('twitter_1', 'Twitter #1'), // this is new
            array('twitter_2', 'Twitter #2'), // this is new
            array('twitter_3', 'Twitter #3'), // this is new
            array('twitter_4', 'Twitter #4'), // this is new
            array('facebook', 'Facebook'),
            array('facebook_1', 'Facebook #1'), // this is new
            array('facebook_2', 'Facebook #2'), // this is new
            array('facebook_3', 'Facebook #3'), // this is new
            array('facebook_4', 'Facebook #4'), // this is new
            array('google', 'Google Plus', 'google-plus'),
            array('google_1', 'Google Plus #1', 'google-plus_1'), // this is new
            array('google_2', 'Google Plus #2', 'google-plus_2'), // this is new
            array('google_3', 'Google Plus #3', 'google-plus_3'), // this is new
            array('google_4', 'Google Plus #4', 'google-plus_4'), // this is new
            array('youtube', 'Youtube'),
            array('linkedin', 'Linkedin'),
            array('instagram', 'instagram'),
            array('whatsapp', 'Whatsapp'),
            array('email', 'Email', 'envelope'),
            array('rss', 'Rss')
        );

        if ($data != null) {
            foreach ($social_list as $social_item) {
                if (isset($data['type']) && $data['type'] == $social_item[0]) {
                    if (!isset($data['url'])) {
                        $data['url'] = '#';
                    }
                    if (!isset($data['target'])) {
                        $data['target'] = '_blank';
                    }
                    $str .= '<li class="social"><a  href="'%20.%20esc_attr($data['url'])%20.%20'" target="' . esc_attr($data['target']) . '"';

                    if (isset($data['tooltip']) && $data['tooltip'] == "yes") {
                        $str .= ' title="' . esc_attr($social_item[1]) . '" class="show-tooltip"';
                        if (isset($data['placement']) && $data['placement'] != "") {
                            $str .= ' data-placement="' . esc_attr($data['placement']) . '"';
                        }
                    }

                    $str .= '><span class="alterna-icon-' . esc_attr($social_item[0]) . '"';

                    if ($data['bg_color'] != "" || $data['color'] != "") {
                        $str .= ' style="';
                        if ($data['bg_color'] != "") {
                            $str .= 'background:' . esc_attr($data['bg_color']) . ';';
                        }
                        if ($data['color'] != "") {
                            $str .= 'color:' . esc_attr($data['color']) . ';';
                        }
                        $str .= '"';
                    }

                    $str .= '><i class="fa fa-' . (isset($social_item[2]) ? esc_attr($social_item[2]) : esc_attr($social_item[0])) . '"></i></span></a></li>';
                }
            }
        } else {
            foreach ($social_list as $social_item) {
                if (penguin_get_options_key('social-' . $social_item[0]) != '') {
                    if (!$topbar) {
                        $str .= '<li class="social"><a title="' . esc_attr($social_item[1]) . '" href="'%20.%20esc_attr(penguin_get_options_key('social-'%20.%20$social_item[0]))%20.%20'" target="' . esc_attr($target) . '" ><span class="alterna-icon-' . esc_attr($social_item[0]) . '"><i class="fa fa-' . (isset($social_item[2]) ? esc_attr($social_item[2]) : esc_attr($social_item[0])) . '"></i></span></a></li>';
                    } else {
                        $str .= '<li class="social"><a href="'%20.%20esc_attr(penguin_get_options_key('social-'%20.%20$social_item[0]))%20.%20'" target="' . esc_attr($target) . '" ><i class="fa fa-' . (isset($social_item[2]) ? esc_attr($social_item[2]) : esc_attr($social_item[0])) . '"></i></a></li>';
                    }
                }
            }
        }

        return $str;
    }
endif;

但是,由于我看不到主题上的变化,所以不起作用。我做错了什么?这是实现这一目标的正确方法?

最佳解决方案

您的孩子主题的 functions.php 中的新功能不能覆盖父项的 functions.php 文件中的函数,除非该函数被写入可插入,即使用 if (!function_exists('alterna_get_social_list'))

根据关于 Child Theme 的文档以及它们如何从 child functions.php 文件继承:

Using functions.php Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent』s functions.php. (Specifically, it is loaded right before the parent』s file.)

[https://codex.wordpress.org/Child_Themes#Using_functions.php][1]

此外,这是非常重要的:

Do not copy the full content of functions.php of the parent theme into functions.php in the child theme.

完全 re-define 的唯一方法是不可插拔的功能是,如果它恰好是通过某种操作添加的,那么您可以先删除调用父主题功能的操作,并添加一个新的操作,调用新功能不同的名字

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。