【问题标题】:wordpress child theme: how to overwrite template .php fileswordpress 子主题:如何覆盖模板 .php 文件
【发布时间】:2015-04-03 05:33:42
【问题描述】:

我在 wordpress 中设置了一个子主题,并且子主题中的 style.css 和 functions.php 工作正常。我现在想用子主题中的版本覆盖其他模板 .php 文件。但是,我似乎无法让这些文件覆盖父主题。

子主题functions.php:

<?php
//enqueue styles
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', 'overwrite' );
function theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );

}
//overwrite template pages
function overwrite() {
    wp_enqueue_script( get_template_directory_uri() . '/inc/structure/hooks.php' );
    wp_enqueue_script( get_template_directory_uri() . '/inc/structure/header.php' );
}

我在 /child-theme/inc/structure/ 中有一个 hooks.php 和 header.php

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    对于子主题,您不需要将任何模板 (PHP) 文件排入队列,只需将 PHP 文件添加到您的子主题,其名称和路径与您在父主题中尝试覆盖的文件完全相同它自然会取代它。

    【讨论】:

    • 感谢您的回复。我一定是在做傻事。主模板目录中的页面仍然可以正常工作,但子文件夹中的任何页面都不能。我的主模板中有:“themes/storefront/inc/structure/hooks.php”,我的子模板目录结构是:“themes/storefront-child/inc/structure/hooks.php”,但更改为钩子。子模板中的 php 文件不会覆盖主模板。
    • 对于钩子,您需要注销原来的钩子并添加一个新的钩子,更多细节见here
    猜你喜欢
    • 2015-01-02
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2015-08-09
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    相关资源
    最近更新 更多