【问题标题】:How to hook into parent functions.php using child theme如何使用子主题挂钩到父functions.php
【发布时间】:2018-01-11 10:10:24
【问题描述】:

父functions.php 中的函数需要包含。我已经编辑了这个文件的调用,但显然它仍然在父目录中。

需要 get_template_directory() 。 '/inc/widgets/team-member.php';

如何修改子functions.php中的文件url?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    在父主题functions.php中添加这段代码

    add_action( 'wp_loaded', 'parent_prefix_load_classes', 10 );
    
    function parent_prefix_load_classes()
    {
       define('MEMBER_FILE_URL', get_template_directory(). '/inc/widgets/team-member.php');
    }
    

    在智利主题中使用此代码

    // Priority 11 to run after the parent theme's loader.
    add_action( 'wp_loaded', 'child_prefix_create_objects', 11 );
    
    function child_prefix_create_objects()
    {
        echo MEMBER_FILE_URL;
    }
    

    【讨论】:

    • 为什么使用require,而不是require_once
    • 另外,这个答案是最好的,需要wordpress.stackexchange.com/a/199425/133553。当然,如果他可以等到所有父主题功能完成工作
    • 为什么要添加定义?在子主题中调用get_template_directory () 仍将返回相同的父目录。我怀疑这是 OP 要求的,但老实说,我不知道 OP 要求什么。
    • @SamvelAleqsanyan stackoverflow.com/questions/2418473/… - 在许多情况下,这并不重要。每当我真正需要 *_once 时,可能是因为我的代码存在缺陷。
    猜你喜欢
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 2017-09-11
    • 2016-08-20
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多