Wordpress functions.php 子主题 [英] Wordpress functions.php child theme

查看:59
本文介绍了Wordpress functions.php 子主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我正在使用 Wordpress 构建一个网站,一切正常,但是现在我需要在functions.php 中添加一些更改,即父文件夹中的原始文件,那么我该怎么做?是否可以复制functions.php子主题中的所有内容,然后添加更改?

Actually i'm building a Website using Wordpress, everything work, but now I need to add some change inside of the functions.php, the original one in the parent folder, so how should I do ? Is it possible to copy everything inside of the functions.php child theme and then add changes ?

由于未来的主题更新,我不想在父文件夹中添加更改.但是告诉我这是不是唯一的选择.

I don't want to add changes in the parent folder because of future theme updates. But tell me if this is the only one option.

不要将父主题的functions.php的全部内容复制到子主题的functions.php中."来自 Wordpress Codex https://codex.wordpress.org/Child_Themes

"Do not copy the full content of functions.php of the parent theme into functions.php in the child theme." is an advice from Wordpress Codex https://codex.wordpress.org/Child_Themes

编辑 2:[已解决] 观看下面的答案.

EDIT 2: [SOLVED] Watch answer below.

推荐答案

子主题 是继承另一个主题(称为父主题)的功能和样式的主题.子主题是推荐修改现有主题的方式.

A child theme is a theme that inherits the functionality and styling of another theme, called the parent theme. Child themes are the recommended way of modifying an existing theme.

如何创建子主题

  1. 创建子主题目录(将子主题命名为父主题并在末尾添加-child的常用方法.)
  2. 创建style.css
  3. 创建functions.php
  1. Create child theme directory (common approach to name child theme as the parent theme plus -child added on the end.)
  2. Create style.css
  3. Create functions.php

样式表必须以以下内容开头(样式表标题):

The stylesheet must begin with the following (the stylesheet header):

/*
Theme Name: XYZ Child Theme
Theme URI: http://example.com/themes/spacious/
Description: XYZ Child theme
Author: ABC
Author URI: http://example.com
Template: XYZ
Version: 1.0
*/
/* =Theme customization starts here
------------------------------------------------------- */

需要注意的几点:

  • 您需要将示例文本替换为与您的主题相关的详细信息.
  • Template 行对应父主题的目录名,因此相应调整.

最后一步是将父和子主题样式表放入functions.php中,如下所示:

The final step is to enqueue the parent and child theme stylesheets inside functions.php like this:

add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );
function enqueue_parent_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}

最后,要激活子主题,请在仪表板中转到外观->主题.查找您创建的子主题并激活它.就是这样.此外,请确保父主题也存在于已安装的主题中,以便子主题正常工作.如果您现在访问您的站点,它看起来应该与激活父主题时一样.

Lastly, to activate the child theme, in the dashboard go to Appearance->Themes. Look for the child theme you created and activate it. That’s it. Also, make sure the parent theme is also present in the installed themes for the child theme to work. If you now visit your site, it should look all same just like when the parent theme was activated.

现在,您可以毫无问题地更改子主题!

NOW, you can make changes in your child theme without any problem!

如果你想修改一个父主题函数,那么你可以简单地将它包含在一个条件标签中,以检查是否已经运行了该名称的函数:

If you want to modify a parent theme function then you can simply enclose it in a conditional tag to check if a function that name has already been run:

<?php
if ( ! function_exists ( 'my_function' ) ) {
    function my_function() {
        // Contents of your function here.
    }
}
?>

因此,如果您将父主题中的函数包含在这样的条件标签中,WordPress 将检查您的子主题中是否存在已运行的同名函数,如果是这种情况,则不会从父主题运行该函数.

So if you enclose the functions in your parent theme in a conditional tag like this, WordPress will check if there is a function with the same name in your child theme that's already been run, and if that's the case it won't run the function from the parent theme.

然后,当您要在子主题中编写要覆盖父主题中的函数的函数时,只需将其命名为与父主题中相同的名称:

Then when you come to write a function in your child theme which you want to override the one in the parent theme, you just give it the same name as the one in the parent theme:

<?php
function my_function() {
    // Contents for your function override here.
}
?>

WordPress 会先运行子主题中的函数,当涉及到父主题中的函数时,它会检查它是否已经存在,因为存在,它不会运行它.

WordPress will run the function in the child theme first, and when it comes to the one in the parent theme, it'll check if it already exists and because it does, it won't run it.

如果您不想触及父functions.php,那么您可以:

  1. 为子主题功能分配更高的优先级:

  1. Assign child theme function higher priority:

add_action( 'init', 'child_function', 15 );   

  • 从钩子中移除父主题函数:

  • Remove parent theme functions from hooks:

    remove_action( 'init', 'parent_function' );
    

  • 这篇关于Wordpress functions.php 子主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆