WordPress修改父代码

发布于 2024-10-25 03:17:06 字数 253 浏览 1 评论 0原文

我被告知子主题是可行的方法,但您应该尽力不要触摸父模板文件并通过操作/过滤器挂钩进行修改。然而,我经常发现需要将

或类似内容插入到没有现有钩子的位置。

有没有更好的方法来修改父代码?我发现最简单的方法就是复制我想要修改的文件,例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐偏执 2024-11-01 03:17:06

有更好的方法来使用子主题,而不涉及从父主题复制文件并破解它们。

主题框架

您以前可能遇到过像 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:

  • providing a clean, solid, well-structured Parent theme
  • providing additional hooks for your markup, and
  • in some cases, providing admin tools that allow you to modify the theme without coding.

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 and functions.php. This allows your theme to keep functioning even when Wordpress and the underlying Parent theme are updated.

猫卆 2024-11-01 03:17:06

这就是我所做的。这不是最干净的解决方案,但它确实有效。

以下 header.php 文件实质上将父主题的 header.php 作为字符串加载,插入代码,保存临时文件,然后将临时文件包含在其自身中以供执行。

$whole_file = file_get_contents(__DIR__ . "/../parent/header.php"); // Load the header file and convert to string
$predecessor_line = '<body id="for-example">'; // We're inserting our code right after this line. As long as the parent theme doesn't update this line, we're good.
$split_file = explode($predecessor_line, $whole_file); // Slice the file at the $predecessor_line (The text used to slice disappears, so be sure to add it again)
$code_to_insert = '<div class="myclass">'; // This is the code you want to insert.
$new_file_content = $split_file[0] . $predecessor_line . $code_to_insert . $split_file[1]; // Piece everything together
$new_file = fopen("_temp_header.php", "w"); // Create a Temporary File
fwrite($new_file, $new_file_content); // Write The Temporary File
fclose($new_file); // Close the Temporary File
include("_temp_header.php"); // Include the Temporary File to execute
unlink("_temp_header.php"); // Delete the Temporary File

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.

$whole_file = file_get_contents(__DIR__ . "/../parent/header.php"); // Load the header file and convert to string
$predecessor_line = '<body id="for-example">'; // We're inserting our code right after this line. As long as the parent theme doesn't update this line, we're good.
$split_file = explode($predecessor_line, $whole_file); // Slice the file at the $predecessor_line (The text used to slice disappears, so be sure to add it again)
$code_to_insert = '<div class="myclass">'; // This is the code you want to insert.
$new_file_content = $split_file[0] . $predecessor_line . $code_to_insert . $split_file[1]; // Piece everything together
$new_file = fopen("_temp_header.php", "w"); // Create a Temporary File
fwrite($new_file, $new_file_content); // Write The Temporary File
fclose($new_file); // Close the Temporary File
include("_temp_header.php"); // Include the Temporary File to execute
unlink("_temp_header.php"); // Delete the Temporary File
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文