在儿童主题中附加WP功能?

时间:2014-11-26 03:20:17

标签: php wordpress function themes append

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

谢谢!

1 个答案:

答案 0 :(得分:0)

不,它无法将代码附加到函数中。

首先,我建议你看一下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. 激活子功能。

  6. 您的代码应如下所示:

    // 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。