如何在functions.php

时间:2015-09-13 20:48:40

标签: php wordpress wordpress-theming parent-child require

我是设计和编码Wordpress主题的新手,最近开始了一个基于悉尼主题的儿童主题,该主题在Wordpress中免费提供。我遇到了一些我无法解决的问题。

我需要自定义并且在更新父主题时需要保持不变,因此需要通过父functions.php调用PHP文件。此文件名为template-tags.php,位于" inc"夹。现在,据说,根据Wordpress codex(我在此之前已经咨询过),子主题上的任何重复文件(functions.php除外)将自动覆盖父主题中的那些。我不知道这是否适用于文件夹内的文件,但我认为这个特定文件是在父functions.php中调用的,这使得它是父母&#39 ;正在加载的文件。我假设这是因为同样适用于我必须在子主题中排队的样式CSS和JS脚本文件,以便获得我自己的自定义版本。

但是,Wordpress编解码器中没有任何内容可以解释如何覆盖已在父functions.php中调用的文件。我无法在其他任何地方找到任何内容(甚至在这些问题中都没有:WordPress child theme override a parent theme includeOveriding parent theme functions with a Child theme in WordPressHow to override a not plugable parent theme function from a non function.php file?Wordpress child theme - functions.php copy - cannot "redeclare"),这对我有所帮助。

所以,总结一下:

  • 父主题有一个" inc"其中包含文件template-tags.php的文件夹。
  • 我的孩子主题也有一个" inc"其中包含文件template-tags.php的文件夹。
  • 父主题functions.php具有以下代码来调用其文件:

    require get_template_directory() . '/inc/template-tags.php';
    
  • 我尝试在我的孩子调用functions.php文件的template-tags.php文件中添加的任何内容都会导致错误,因为很明显WP不需要同样的事情两次。

没有评论父母functions.php中的电话,这对我来说似乎不是一个实用的解决方案,但更多是解决方法,我不知道还有什么可做的

我很感激此时的任何意见。提前谢谢!

2 个答案:

答案 0 :(得分:11)

Hello @ QuestionerNo27,

自从你提出这个问题以来已经有很长一段时间了,但是在我遇到同样的问题时,我也一直在寻找2天的答案。有答案,简单如“你好”:

  1. 您不需要子主题中的inc文件夹,也不需要template-tags.php文件。
  2. 您只需要从父template-tags.php复制要覆盖的功能,然后将其粘贴到您的子主题functions.php中 在我的情况下,我想要从我的父function mytheme_posted_on()
  3. 覆盖template-tags.php

    它现在正在运作。感谢https://wordpress.org/support/topic/inc-folder-in-child-theme(Stephencottontail回答)

答案 1 :(得分:1)

我遇到了类似的问题,我希望在template-tags.php中更改子主题中标题的布局。

对我有用的解决方案是在我的子主题的functions.php文件中使用以下行: 需要get_stylesheet_directory()。 '/inc/template-tags.php';

我在functions.php中使用了Parent中的原始行: 需要get_template_directory()。 '/inc/template-tags.php';

使用函数get_template_directory()无效,因为此函数将始终返回父主题目录。

使用函数get_stylesheet_directory()有效,因为它检索Child Theme目录。

是的我知道函数“get_stylesheet_directory()”的名称不是很直观,但它实际上会返回子主题目录位置。

通过在线搜索,需要一段时间才能解决这个问题。我在网上找到了这个参考资料,帮助我进行搜索:How to override a parent file in a child Wordpress theme when parent file is being required in functions.php