希望有人能帮忙,我正在寻找一些关于如何覆盖/替换加载在父主题中的一些函数的建议,并将它们移动到子主题中,这样我们就可以修改子主题中的文件本身。
我们将将结构和文件“复制”到子主题中,但无法找到“卸载”原始父主题文件的方法,然后加载要更改的子主题文件。
本质上,我们将在子主题中复制和修改下面列出的所有“要求”文件,但需要找到某种方法来覆盖父级functions.php。
我们尝试过多种方法来做到这一点,但似乎到目前为止还不能让它发挥作用。
这是当前的父FUNCTIONS.PHP:
tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support( 'title-tag' );
/*
* Enable support for Post Thumbnails on posts and pages.
*
* @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
*/
add_theme_support( 'post-thumbnails' );
// This theme uses wp_nav_menu() in one location.
register_nav_menus( array(
'primary' => esc_html__( 'MOTO Main Menu', 'moto' ),
'pre_header' => esc_html__( 'Preheader Menu(You have to enable Preheader from MOTO Options panel to view this menu.)', 'moto' )
) );
/*
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
*/
add_theme_support( 'html5', array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
) );
/*
* Enable support for Post Formats.
* See http://codex.wordpress.org/Post_Formats
*/
add_theme_support( 'post-formats', array(
'aside',
'image',
'video',
'quote',
'link',
) );
// Set up the WordPress core custom background feature.
add_theme_support( 'custom-background', apply_filters( 'moto_custom_background_args', array(
'default-color' => 'ffffff',
'default-image' => '',
) ) );
add_theme_support( 'woocommerce' );
global $pagenow;
if ( is_admin() && 'themes.php' == $pagenow && isset( $_GET['activated'] ) ) {
wp_redirect(admin_url("options-general.php?page=moto-system-status")); // Your admin page URL
exit();
}
}
endif; // moto_setup
add_action( 'after_setup_theme', 'moto_setup' );
/**
* Set the content width in pixels, based on the theme's design and stylesheet.
*
* Priority 0 to make it available to lower priority callbacks.
*
* @global int $content_width
*/
function moto_content_width() {
$GLOBALS['content_width'] = apply_filters( 'moto_content_width', 640 );
}
add_action( 'after_setup_theme', 'moto_content_width', 0 );
/**
* Register widget area.
*
* @link http://codex.wordpress.org/Function_Reference/register_sidebar
*/
function moto_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'Sidebar', 'moto' ),
'id' => 'sidebar-1',
'description' => esc_html__( 'Defualt Sidebar', 'moto' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
register_sidebar( array(
'name' => esc_html__( 'Shop Sidebar', 'moto' ),
'id' => 'shopsidebar',
'description' => esc_html__( 'Shop Sidebar Show Only Shop pages', 'moto' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
register_sidebar( array(
'name' => esc_html__( 'Footer Sidebar - 1', 'moto' ),
'id' => 'footer-1',
'description' => esc_html__( 'Footer Sidebar - 1', 'moto' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
register_sidebar( array(
'name' => esc_html__( 'Footer Sidebar - 2', 'moto' ),
'id' => 'footer-2',
'description' => esc_html__( 'Footer Sidebar - 2', 'moto' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
}
add_action( 'widgets_init', 'moto_widgets_init' );
/**
* Implement the Custom Header feature.
*/
require get_template_directory() . '/function/custom-header.php';
/**
* Custom template tags for this theme.
*/
require get_template_directory() . '/function/template-tags.php';
/**
* Custom functions that act independently of the theme templates.
*/
require get_template_directory() . '/function/extras.php';
/**
* Customizer additions.
*/
require get_template_directory() . '/function/customizer.php';
/**
* Load Jetpack compatibility file.
*/
require get_template_directory() . '/function/jetpack.php';
require_once get_template_directory() . '/include/aq_resizer.php';
require_once get_template_directory() . '/include/moto-sys-req.php';
require_once get_template_directory() . '/include/moto-enqueue.php';
require_once get_template_directory() . '/include/moto-functions.php';
require_once get_template_directory() . '/include/theme_plugin/plugin-activate-config.php';
require_once get_template_directory() . '/include/wordpress-reset.php';有什么建议吗?
提前谢谢你。
发布于 2018-07-29 09:46:55
不能用子主题替换整个任意文件。
WordPress将自动查看子主题以替换模板层次中的模板,以及一些附加文件(如searchform.php或comments.php ),但父主题加载的任何其他文件只有在父主题作者构建为是的情况下才能在子主题中替换。这包括functions.php或模板中包含的任何文件。
对于由子主题可替换的文件,必须使用支持的函数加载该文件。例如,如果主题(如您的主题)加载这样的文件:
require_once get_template_directory() . '/include/aq_resizer.php';那么子主题就不能取代它。这是因为路径的'/include/aq_resizer.php'部分根本没有通过WordPress。这是一个正常的PHP,WordPress无法拦截。此外,get_template_directory()只会成为父主题的目录,而不是一个活动的子主题。
为了能够替换子主题中的整个文件,父主题必须用以下函数之一加载它:
require_once get_theme_file_path( '/include/aq_resizer.php' );因为文件路径是作为参数传递给get_theme_file_path()的,而不仅仅是直接传递给PHP的连接字符串,所以函数可以先查看子主题,然后才会这样做。
对于模板,如果父主题使用get_template_part(),如下所示:
get_template_part( 'partials/content' );然后,子主题可以创建partials/content.php来替换它,但是如果父主题使用include partials/content.php,那么就不可能用子主题替换。
get_theme_file_path()比get_template_part() (3.0中引入的)更新得多(在4.7中引入),因此在旧主题中不那么常见,也不存在。它也不为人所知。
在您的代码中,父主题不使用这两种方法,因此不可能替换整个文件。这意味着您需要逐个替换单个函数。执行此操作的方法将取决于如何使用该函数。
If父主题用 add_action()挂钩函数
如果父主题钩住要用add_action()替换的函数,则可以通过在子主题中创建函数的新版本(使用不同的名称)来替换函数,用remove_action()解锁原始函数,然后将新函数与add_action()挂钩:
remove_action( 'hook_name', 'parent_theme_function_name' );
add_action( 'hook_name', 'child_theme_function_name' );If父主题使用模板文件中的函数
如果父主题具有要替换的函数,并且在模板文件中使用该函数,则需要在子主题中(使用不同的名称)创建函数的新版本,然后在子主题中替换模板文件,然后在子主题的模板版本中用新函数替换最初的函数用法。
If父主题函数是可插入的
父主题加载在子主题之前。这意味着主题开发人员可以通过包装然后在function_exists()检查中使函数被子主题替换。这意味着您可以用自己的函数替换函数定义,它不会引起冲突,因为父主题不会尝试重新定义它,如果您已经这样做了。
您的代码有这样一个示例:moto_setup()函数在此检查中:
if ( ! function_exists( 'moto_setup' ) ) :
endif;这使得moto_setup()函数“可插拔”,这意味着您可以在您的子主题中定义一个moto_setup()函数,并且父主题将使用它。
您的主题中的其他函数是否是“可插拔的”,这样说是不可能不看代码的。
结论
发布于 2018-07-29 09:15:02
简单地使用
function moto_setup() {
// Your new moto_setup function
}而不检查函数是否存在。该函数将覆盖父主题中声明的moto_setup()。
发布于 2018-08-11 03:25:52
好的,在与Moto v3的开发人员一起工作了几周之后,他们无法让子主题按其应有的方式工作。
主题不遵循“最佳实践”编码,如果没有在functions.php文件中进行广泛的额外编码,子主题就无法覆盖父主题,而且总体上有点混乱。
我们已经放弃了。
https://wordpress.stackexchange.com/questions/309840
复制相似问题