主题功能

笔记: Functions.php经典主题、块主题和子主题均可使用。

functions.php文件是您向 WordPress 主题添加独特功能的地方。它可用于挂钩 WordPress 的核心功能,使您的主题更加模块化、可扩展性和功能性。

是什么functions.php

functions.php文件的行为类似于 WordPress 插件,可向 WordPress 站点添加特性和功能。您可以使用它来调用 WordPress 函数并定义您自己的函数。

笔记: 使用插件或functions.php. 如果您正在创建无论网站看起来如何都应该可用的新功能,最好将它们放在插件中

使用 WordPress 插件或使用functions.php.

一个 WordPress 插件:

  • 需要特定的、唯一的标题文本;
  • 存放在wp-content/plugins中,通常在一个子目录中;
  • 仅在激活时在页面加载时执行;
  • 适用于所有主题;和
  • 应该有一个单一的目的——例如,提供搜索引擎优化功能或帮助备份。

同时,一个functions.php文件:

  • 不需要唯一的标题文本;
  • 存储在 wp-content/themes 中主题的子目录中;
  • 仅在活动主题的目录中执行;
  • 仅适用于该主题(如果更改主题,则无法再使用该功能);和
  • 可以有许多代码块用于许多不同的目的。

functions.php 每个主题都有自己的函数文件,但实际上只运行活动主题中的代码 。如果您的主题已经有一个函数文件,您可以向其中添加代码。functions.php如果没有,您可以创建一个名为添加到主题目录的纯文本文件,如下所述。

子主题可以有自己的functions.php文件。将函数添加到子函数文件是一种无风险的修改父主题的方法。这样,当父主题更新时,你就不用担心你新添加的功能消失了。

笔记:尽管子主题functions.php由 WordPress 在父主题之前加载functions.php,但它不会_覆盖_ 它。子主题functions.php 可用于增强或替换父主题的功能。同样, 在任何插件文件加载后functions.php加载。

有了functions.php你可以:

  • 使用 WordPress 挂钩。例如,您可以使用过滤excerpt_length器更改帖子摘录长度(默认为 55 个字)。
  • 使用 启用 WordPress 功能add_theme_support()。例如,打开帖子缩略图、帖子格式和导航菜单。
  • 定义您希望在多个主题模板文件中重用的功能。

警告:
在 WordPress 中,当两个或多个函数、类或变量具有相同的名称时,可能会发生命名冲突。这可能会导致 WordPress 站点出现错误或意外行为。主题开发人员和插件开发人员都有责任避免各自代码中的命名冲突。

主题开发人员应确保他们的函数、类和变量具有唯一的名称,不会与 WordPress 核心或其他插件使用的名称冲突。他们还应该在函数和类名称前加上唯一标识符,例如主题名称或缩写,以尽量减少命名冲突的可能性。

例子

下面是一些示例,您可以在 functions.php 文件中使用这些示例来支持各种功能。如果您选择将这些示例提交到 WordPress.org 主题目录,则这些示例中的每一个都可以在您的主题中使用。

主题设置

许多主题功能应该包含在“设置”功能中,该功能在您的主题被激活时最初运行。如下所示,这些功能中的每一个都可以添加到您的functions.php文件中以激活推荐的 WordPress 功能。

笔记:使用您的主题名称为您的函数命名空间很重要。以下所有示例都使用 myfirsttheme_ as their namespace,应根据您的主题名称进行自定义。

要创建这个初始函数,请启动一个名为 的新函数myfirsttheme_setup(),如下所示:

if ( ! function_exists( 'myfirsttheme_setup' ) ) :
/**
 * Sets up theme defaults and registers support for various WordPress  
 * features.
 *
 * It is important to set up these functions before the init hook so
 * that none of these features are lost.
 *
 *  @since MyFirstTheme 1.0
 */
function myfirsttheme_setup() { ... }

注意:在上面的示例中,函数 myfirsttheme_setup 已启动但未关闭。一定要关闭你的功能。

自动提要链接

默认情况下,自动提要链接启用帖子和评论 RSS 提要。这些提要将自动显示<head>add_theme_support()可以在经典主题中调用它们。此功能会自动为块主题启用,并且不需要在主题设置期间包含在内。

add_theme_support( 'automatic-feed-links' );

导航菜单

在经典主题中,自定义导航菜单允许用户在菜单管理面板中编辑和自定义菜单,为用户提供一个拖放界面来编辑其主题中的各种菜单。

中可以设置多个菜单functions.php。它们可以添加 usingregister_nav_menus()或插入到主题中 using wp_nav_menu(),如本手册后面所述。如果您的主题允许多个菜单,您应该使用一个数组。虽然某些主题没有自定义导航菜单,但建议您启用此功能以便于自定义。

register_nav_menus( array(
    'primary'   => __( 'Primary Menu', 'myfirsttheme' ),
    'secondary' => __( 'Secondary Menu', 'myfirsttheme' )
) );

您定义的每个菜单都可以在以后使用wp_nav_menu()并使用指定的名称(即主要)作为theme_location参数来调用。

在块主题中,您改用导航块。

加载文本域

通过使主题中的字符串可用于翻译,可以将主题翻译成多种语言。为此,您必须使用 load_theme_textdomain(). 有关使您的主题可用于翻译的更多信息,请阅读 国际化 部分。

load_theme_textdomain( 'myfirsttheme', get_template_directory() . '/languages' );

发布缩略图

帖子缩略图和特色图片 允许您的用户选择一张图片来代表他们的帖子。您的主题可以根据其设计决定如何显示它们。例如,您可以选择在存档视图中显示每个帖子的帖子缩略图。或者,您可能想在主页上使用大型特色图片。此功能会自动为块主题启用,并且不需要在主题设置期间包含在内。

add_theme_support( 'post-thumbnails' );

发布格式

帖子格式允许用户以不同的方式格式化他们的帖子。这对于允许博主根据帖子的内容选择不同的格式和模板很有用。add_theme_support()也用于发布格式。这是推荐的

add_theme_support( 'post-formats',  array( 'aside', 'gallery', 'quote', 'image', 'video' ) );

了解有关帖子格式的更多信息。

块主题中的主题支持

在块主题中,自动启用以下主题支持:

add_theme_support( 'post-thumbnails' );
add_theme_support( 'responsive-embeds' );
add_theme_support( 'editor-styles' );
add_theme_support( 'html5', array( 'style','script' ) );
add_theme_support( 'automatic-feed-links' ); 

初始设置示例

包括所有上述功能将为您提供functions.php如下所示的文件。为了将来的清晰起见,添加了代码注释。

如本示例底部所示,您必须添加必需的add_action() 语句以确保myfirsttheme_setup加载函数。

if ( ! function_exists( 'myfirsttheme_setup' ) ) :
	/**
	 * Sets up theme defaults and registers support for various
	 * WordPress features.
	 *
	 * Note that this function is hooked into the after_setup_theme
	 * hook, which runs before the init hook. The init hook is too late
	 * for some features, such as indicating support post thumbnails.
	 */
	function myfirsttheme_setup() {

    /**
	 * Make theme available for translation.
	 * Translations can be placed in the /languages/ directory.
	 */
		load_theme_textdomain( 'myfirsttheme', get_template_directory() . '/languages' );

		/**
		 * Add default posts and comments RSS feed links to <head>.
		 */
		add_theme_support( 'automatic-feed-links' );

		/**
		 * Enable support for post thumbnails and featured images.
		 */
		add_theme_support( 'post-thumbnails' );

		/**
		 * Add support for two custom navigation menus.
		 */
		register_nav_menus( array(
			'primary'   => __( 'Primary Menu', 'myfirsttheme' ),
			'secondary' => __( 'Secondary Menu', 'myfirsttheme' ),
		) );

		/**
		 * Enable support for the following post formats:
		 * aside, gallery, quote, image, and video
		 */
		add_theme_support( 'post-formats', array( 'aside', 'gallery', 'quote', 'image', 'video' ) );
	}
endif; // myfirsttheme_setup
add_action( 'after_setup_theme', 'myfirsttheme_setup' );

内容宽度

在经典主题中,内容宽度被添加到您的functions.php文件以确保没有内容或资产破坏网站的容器。内容宽度设置添加到您站点的任何内容的最大允许宽度,包括上传的图像。在下面的示例中,内容区域的最大宽度为 800 像素。没有内容会比这更大。

if ( ! isset ( $content_width) ) {
    $content_width = 800;
}

包含 theme.json 配置文件的主题不需要在 functions.php 中包含变量。相反,内容宽度被添加到 theme.json 中的布局设置。您可以在高级部分了解有关使用 theme.json 的更多信息。

其他特性

您还可以在functions.php. 下面列出了一些最常见的功能。单击并了解有关这些功能的更多信息。

  • 自定义标题 - 经典主题
  • 侧边栏(小部件区域)-经典主题
  • 自定义背景**-经典主题**
  • 标题标签**-经典主题**
  • 添加编辑器样式
  • HTML5 - 经典主题

你的_functions.php_文件

如果您选择包括上面列出的所有函数,这就是您的_functions.php_的样子。已参考上述内容对其进行了评论。

/**
 * MyFirstTheme's functions and definitions
 *
 * @package MyFirstTheme
 * @since MyFirstTheme 1.0
 */

/**
 * First, let's set the maximum content width based on the theme's
 * design and stylesheet.
 * This will limit the width of all uploaded images and embeds.
 */
if ( ! isset( $content_width ) ) {
	$content_width = 800; /* pixels */
}

if ( ! function_exists( 'myfirsttheme_setup' ) ) :

	/**
	 * Sets up theme defaults and registers support for various
	 * WordPress features.
	 *
	 * Note that this function is hooked into the after_setup_theme
	 * hook, which runs before the init hook. The init hook is too late
	 * for some features, such as indicating support post thumbnails.
	 */
	function myfirsttheme_setup() {

		/**
		 * Make theme available for translation.
		 * Translations can be placed in the /languages/ directory.
		 */
		load_theme_textdomain( 'myfirsttheme', get_template_directory() . '/languages' );

		/**
		 * Add default posts and comments RSS feed links to <head>.
		 */
		add_theme_support( 'automatic-feed-links' );

		/**
		 * Enable support for post thumbnails and featured images.
		 */
		add_theme_support( 'post-thumbnails' );

		/**
		 * Add support for two custom navigation menus.
		 */
		register_nav_menus( array(
			'primary'   => __( 'Primary Menu', 'myfirsttheme' ),
			'secondary' => __( 'Secondary Menu', 'myfirsttheme' ),
		) );

		/**
		 * Enable support for the following post formats:
		 * aside, gallery, quote, image, and video
		 */
		add_theme_support( 'post-formats', array( 'aside', 'gallery', 'quote', 'image', 'video' ) );
	}
endif; // myfirsttheme_setup
add_action( 'after_setup_theme', 'myfirsttheme_setup' );