简体   繁体   English

WordPress:编辑子主题

[英]Wordpress: editing child theme

I want to edit some stuff on my website. 我想在我的网站上编辑一些内容。 I'm using a child theme and I plan to change some CSS and do some structural changes. 我使用的是儿童主题,我计划更改一些CSS并进行一些结构更改。 I know the CSS is easy to change, as I only need to call the names of the classes or IDs and give the new values. 我知道CSS很容易更改,因为我只需要调用类或ID的名称并提供新值即可。

But, if I want to change things around in html, do I need to copy the whole code? 但是,如果我想更改html中的内容,是否需要复制整个代码? Will I lose every change when the theme updates (as in: i copied and pasted the older version and now I need to copy and paste the new version )? 主题更新时是否会丢失所有更改(如:复制并粘贴旧版本 ,现在需要复制并粘贴新版本 )?

I don't know much about the names of things and how to describe them properly. 我对事物的名称以及如何正确描述它们了解不多。

Make copies of the theme files in the parent theme you want to modify - such as index.php, category.php, etc - and move those to the child theme and edit them. 在要修改的父主题中制作主题文件的副本(例如index.php,category.php等),然后将其移动到子主题并进行编辑。 The copies in the child theme will be used by WordPress rather than the same-named files in the parent. 子主题中的副本将由WordPress使用,而不是父主题中的同名文件。 This includes file in folders, ie /css/style.css , so duplicate the file structure in the child theme, if needed. 这包括文件夹中的文件,即/css/style.css ,因此,如果需要,可以在子主题中复制文件结构。

The child theme will continue to use those files, even if/when the parent theme get updated. 子主题将继续使用这些文件,即使/当父主题被更新时。 And that also means your child theme edits won't disappear, unless there are major structural/functional changes to the parent theme. 这也意味着您的子主题编辑将不会消失,除非父主题有重大的结构/功能更改。

One exception to child theme file usage is functions.php : 子主题文件用法的一个例外是functions.php

Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. 与style.css不同,子主题的functions.php不会覆盖其父主题中的对应对象。 Instead, it is loaded in addition to the parent's functions.php. 相反,它是在父对象的functions.php之外加载的。 (Specifically, it is loaded right before the parent's file.) (具体地说,它是在父文件之前加载的。)

See http://codex.wordpress.org/Child_Themes 参见http://codex.wordpress.org/Child_Themes

Taken from Wordpress Doc - Child Themes : 取自Wordpress Doc-儿童主题

If you want to change more than just the stylesheet, your child theme can override any file in the parent theme: simply include a file of the same name in the child theme directory, and it will override the equivalent file in the parent theme directory when your site loads. 如果您不仅要更改样式表,则您的子主题可以覆盖父主题中的任何文件:只需在子主题目录中包含相同名称的文件,并且当父主题目录中的同名文件覆盖时,您的网站加载。 For instance, if you want to change the PHP code for the site header, you can include a header.php in your child theme's directory, and that file will be used instead of the parent theme's header.php. 例如,如果要更改网站标题的PHP代码,则可以在子主题的目录中包含header.php,并且将使用该文件代替父主题的header.php。

Wordpress Child Theme can't have a child theme itself. Wordpress儿童主题本身不能具有儿童主题。 It's not supported by Wordpress. Wordpress不支持。 I had the same problem before and after some searches I found this article . 在进行一些搜索前后,我遇到了同样的问题,我发现了这篇文章

It explains about creating a grandchild theme; 它解释了如何创建孙主题。 from the article: 从文章:

The solution is surprisingly simple. 解决方案非常简单。 Instead of editing the child theme, create a grandchild theme. 创建子孙主题,而不是编辑子主题。 It's very similar to creating a child theme, except you do it via a plugin. 这与创建子主题非常相似,只不过您是通过插件来完成的。 You add your custom functions to the plugin, just as you normally would in functions.php (though remember your plugin will be called much earlier than functions.php, so you'll need to make sure that any code in your plugin only runs when an action is fired). 您将自定义函数添加到插件中,就像通常在functions.php中一样(尽管请记住,您的插件的调用要早于functions.php,因此您需要确保插件中的任何代码仅在动作被触发)。 Use the wp_register_style and wp_enqueue_style functions to add your CSS (by default a grandchild theme will add your CSS to the CSS of the parent or child theme, but you can change this behaviour by using the wp_dequeue_style function). 使用wp_register_style和wp_enqueue_style函数添加CSS(默认情况下,孙子主题会将CSS添加到父主题或子主题的CSS,但是您可以使用wp_dequeue_style函数更改此行为)。 Finally filter the result of the get_query_template function to control which PHP files are executed when a page is requested. 最后,过滤get_query_template函数的结果以控制在请求页面时执行哪些PHP文件。

It's not really a child theme of a child theme, but a plugin including calls to add_filters or something similar to override the behaviour of the child theme. 它实际上不是子主题的子主题,而是一个包含对add_filters调用或类似的东西来覆盖子主题行为的插件。 In this way upgrading child / parent themes will not affect the customisation you've made. 这样,升级子主题/父主题不会影响您进行的自定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM