问题描述

如果我使用诸如创世纪的流行框架之一,是否可以创建孙子主题?

例如,我想使用创世纪作为我所有网站的支柱。创世纪被称为框架,但实际上是一个父主题。

然后,我在 StudioPress 中选择一个 Genesis 儿童主题,然后继续为我的每个网站定制。我会为我的所有网站创建一个孙子主题,我该怎么办?

最佳解决方案

是和否

您可以创建一个指定另一个子主题作为父项的子主题,WordPress 会尝试使用它。

然而

你会遇到问题,更不用说核心开发商明确表示这不是理想的行为,而且他们不会努力支持孙子主题。

例如,WP API 区分样式表和模板 URL /目录,其中样式表总是引用活动主题,模板是指父主题,但如果包含祖父母主题,get_template_directory_uri 是否引用父项或祖父母?许多 API 调用现在是模糊的,不同的人会期待不同的行为,包括核心的代码。您还需要加载父母或祖父母的 functions.php,并确保按正确的顺序完成。

这也被认为是非常糟糕的做法。如果你需要孙子主题,那么你的方法已经错了,你需要退后一步,并且 re-evaluate 的东西。

我建议你避免孙子主题的概念,会导致更多的问题。相反,更多的钩子可以过滤孩子主题中的操作和模块性,使您能够保持子主题共享组件相同,并让您的分支/分支很难。尝试将公共元素移动到 svn 外部/git 子模块中。

还有_s 模型,你把你的小孩主题作为一个基础,并分叉,而不是把它作为一个父母,你打算在副本上工作,而不是一个孩子/覆盖。

次佳解决方案

我没有完全测试下面描述的方法,也没有正常的”grandchild” 主题,但是给出了 template_include 过滤器的功能:

    /*
    Plugin Name: Grandchild Themes
    Plugin URI: http://www.who-cares.com/
    Description: A concept for Grandchild themes Plugin
    Author: See Plugin URI
    Version: 0.0.0.0.1-Alpha
    */

    add_action('wp_head', 'grnd_chld_add_headers', 0);
    add_action('init', 'grnd_chld_add_css');

    // Load template if exists.
function grandchild_template_include( $template ) {

    if ( file_exists( untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/grnd_chld_templates/' . basename( $template ) ) )
        $template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/grnd_chld_templates/' . basename( $template );

    return $template;
}

// This is the actual filter that we want .
add_filter( 'template_include', 'grandchild_template_include', 11 );

    function grnd_chld_add_headers () {
        wp_enqueue_style('my_grandchild_style');
    }

    function grnd_chld_add_css() {
        $stamp = @filemtime(plugin_dir_path(__FILE__).'/style.css'); // easy versioning
        wp_register_style ('my_grandchild_style', plugins_url('style.css', __FILE__).'', array(), $stamp);
    }

    // From here , what you got is like a normal functions.php.

您也可以以类似的方式尝试更具体的过滤器,例如 archive_template

add_filter ('archive_template', create_function ('', 'return plugin_dir_path(__FILE__)."archive.php";'));

所有这一切表示和做的,我不知道这是做什么的最好办法。

参考文献

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