我试图在子主题中覆盖以下代码。我想从子文件夹加载文件/inc/soundtheme-admin.php。我在child-theme/functions.php中复制了相同的代码片段,也复制了inc/soundtheme-admin.php。我还将get_template_directory()替换为get_stylesheet_directory(),但它仍然加载父文件。
请在我犯错误的地方纠正我。提前谢谢。
// Code snippet from parent-theme/functions.php
if( function_exists('acf_add_options_page') ) {
require get_template_directory() . '/inc/soundtheme-admin.php';
$paper_themes =
acf_add_options_page(array(
'page_title' => '',
'menu_title' => 'Sound Theme',
'menu_slug' => 'theme-general-settings',
'capability' => 'edit_posts',
'redirect' => false,
'autoload' => false,
'icon_url' => 'dashicons-carrot'
));
acf_add_options_sub_page(array(
'page_title' => '',
'menu_title' => 'Sound Options',
'parent_slug' => 'theme-general-settings',
));
acf_add_options_sub_page(array(
'page_title' => '',
'menu_title' => 'Sound Layouts',
'parent_slug' => 'theme-general-settings',
));
acf_add_options_sub_page(array(
'page_title' => '',
'menu_title' => 'Sound Code',
'parent_slug' => 'theme-general-settings',
));
acf_add_options_sub_page(array(
'page_title' => '',
'menu_title' => 'Sound Supports',
'parent_slug' => 'theme-general-settings',
));
}发布于 2017-09-15 20:54:42
子主题中的函数将在父主题中的函数之前加载。这意味着,如果父主题和子主题都具有名为my_function()的函数,它们执行类似的工作,则父主题中的函数将加载到最后,这意味着它将覆盖子主题中的函数。
此外:
如果您没有使用您自己的父主题,或者您使用的第三方主题没有可插拔的函数,那么您将需要另一种方法。
当您编写函数时,您可以为它们分配一个优先级,这将告诉WordPress何时运行它们。在将函数添加到操作或筛选器挂钩时,可以这样做。然后,WordPress将按照优先级的升序运行附加到给定钩子上的函数,因此那些数字较高的函数将在最后运行。
让我们假设父主题中的函数不可插拔,如下所示:
<?php
function parent_function() {
// Contents for your function here.
}
add_action( 'init', 'parent_function' );
?>这意味着子主题中的函数如下所示:
<?php
function child_function() {
// Contents for your function here.
}
add_action( 'init', 'child_function', 15 );
?>这应该让你开始..。
发布于 2017-09-15 22:04:31
上面的评论肯定有帮助。声明位于acf_add_options_page函数的不同文件中,您还需要包含该文件的子函数。
我只是从父函数s.php文件中复制了if块中的代码,并将其粘贴到子函数s.php文件中,包括必需的文件。对我起作用了。
希望它也能帮到你。
谢谢。
https://wordpress.stackexchange.com/questions/280161
复制相似问题