WordPress修改父代码
我被告知子主题是可行的方法,但您应该尽力不要触摸父模板文件并通过操作/过滤器挂钩进行修改。然而,我经常发现需要将
或类似内容插入到没有现有钩子的位置。有没有更好的方法来修改父代码?我发现最简单的方法就是复制我想要修改的文件,例如 header.php,然后进行我需要的更改。当然,如果父主题更新了,我的 header.php 将过时,并且查找更改将是一件痛苦的事情!
I've been told child themes are the way to go, but that you should try your best to not touch the parent template files and make modifications through action/filter hooks. Often, however, I find that I need to insert a <div class="myclass">
or similar into a place where there is no existing hook.
Is there a better way to modify parent code? I find the easiest thing to do is just copy over the file I want to modify, such as header.php, and then making the changes I need. Of course, if the parent theme is updated, my header.php will be out of date, and finding the changes will be a pain!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有更好的方法来使用子主题,而不涉及从父主题复制文件并破解它们。
主题框架
您以前可能遇到过像 Thesis、Carrington 或 Thematic 这样的主题框架。
主题框架背后的想法是,它将通过以下方式为您的子主题开发提供灵活的基础:
http://codex.wordpress.org/Theme_Frameworks
功能覆盖
有了主题框架,很容易覆盖使用
functions.php
的现有函数。这使您可以使用自己的自定义代码替换页眉和页脚等常见功能,还可以使用所选主题框架中不包含的功能来扩展主题。以下是主题框架的一些示例(我在最近的项目中使用了主题):
因此,您在子主题中需要修改的就是
style.css
和函数.php
。这使得您的主题即使在 WordPress 和底层父主题更新时也能继续运行。There are better ways to use child themes that don't involve copying files from the parent theme and hacking them.
Theme frameworks
You will probably have encountered theme frameworks like Thesis, Carrington or Thematic before.
The idea behind a theme framework is that it will give you a flexible foundation for child theme development by:
http://codex.wordpress.org/Theme_Frameworks
Function overrides
With a theme framework, it is easy to override existing functions using your
functions.php
. That allows you to replace common functionality like headers and footers with your own custom code, and also to extend the theme with functions that are not in the chosen theme framework.Here are some examples for the Thematic framework (I've been using Thematic on a recent project):
So all you should have to modify in your child theme is your
style.css
andfunctions.php
. This allows your theme to keep functioning even when Wordpress and the underlying Parent theme are updated.这就是我所做的。这不是最干净的解决方案,但它确实有效。
以下 header.php 文件实质上将父主题的 header.php 作为字符串加载,插入代码,保存临时文件,然后将临时文件包含在其自身中以供执行。
Here's what I did. It's not the cleanest solution, but it works.
The following header.php file essentially loads the parent theme's header.php as a string, inserts code, saves a temporary file, and then includes the temporary file in itself for execution.