【问题标题】:Append to WP function in child theme?在子主题中附加到 WP 功能?
【发布时间】:2015-01-24 06:51:19
【问题描述】:

我有一个 Wordpress 主题,它有几个需要修改的功能(通过子主题)。我宁愿不完全替换该功能,以防将来更新。是否可以将代码附加到函数?

谢谢!

【问题讨论】:

    标签: php wordpress function themes append


    【解决方案1】:

    不,不可能将代码附加到函数中。

    首先,我建议您查看Child themes 文档。

    子主题的行为很大程度上取决于您在哪里编辑函数。如果您使用functions.php,Wordpres 声明:

    与 style.css 不同,子主题的 functions.php 不会覆盖 它来自父级。相反,它除了加载 父母的functions.php。 (具体来说,它是在之前加载的 父母的文件。)

    所以你可以做的是覆盖父函数,如here

    1. 从父主题复制(完整)您要覆盖的功能。

    2. 将其粘贴到您的子主题文件夹根目录下的 functions.php 中。如果functions.php不存在,创建它。

    3. 将函数从 parent_theme_function 重命名为 child_theme_function。

    4. 停用父函数。

    5. 激活子函数。

    您的代码应如下所示:

    // Removes thematic_blogtitle from the thematic_header phase
    function remove_thematic_actions() {
        remove_action('thematic_header','thematic_blogtitle',3);
    }
    // Call 'remove_thematic_actions' during WP initialization
    add_action('init','remove_thematic_actions');
    
    // Add our custom function to the 'thematic_header' phase
    add_action('thematic_header','fancy_theme_blogtitle', 3);
    

    在本例中,函数 thematic_blogtitle 被从函数中移除,并被fancy_theme_blogtitle 取代。

    【讨论】:

    • 如果该功能尚未分配给操作,我该如何停用该功能?有没有办法将父函数指向子函数?
    • 您可以使用上面的sn-p代码来停用该功能。该代码应该可以解决您的问题,尝试一下并告诉我们。
    猜你喜欢
    • 2012-06-19
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2017-01-16
    • 2018-05-06
    • 1970-01-01
    相关资源
    最近更新 更多