【发布时间】:2015-01-24 06:51:19
【问题描述】:
我有一个 Wordpress 主题,它有几个需要修改的功能(通过子主题)。我宁愿不完全替换该功能,以防将来更新。是否可以将代码附加到函数?
谢谢!
【问题讨论】:
标签: php wordpress function themes append
我有一个 Wordpress 主题,它有几个需要修改的功能(通过子主题)。我宁愿不完全替换该功能,以防将来更新。是否可以将代码附加到函数?
谢谢!
【问题讨论】:
标签: php wordpress function themes append
不,不可能将代码附加到函数中。
首先,我建议您查看Child themes 文档。
子主题的行为很大程度上取决于您在哪里编辑函数。如果您使用functions.php,Wordpres 声明:
与 style.css 不同,子主题的 functions.php 不会覆盖 它来自父级。相反,它除了加载 父母的functions.php。 (具体来说,它是在之前加载的 父母的文件。)
所以你可以做的是覆盖父函数,如here:
从父主题复制(完整)您要覆盖的功能。
将其粘贴到您的子主题文件夹根目录下的 functions.php 中。如果functions.php不存在,创建它。
将函数从 parent_theme_function 重命名为 child_theme_function。
停用父函数。
激活子函数。
您的代码应如下所示:
// 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 取代。
【讨论】: