問題描述
我試圖覆蓋一個主題在 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。