企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
functions.php文件是您向WordPress主题添加独特功能的地方。 它可以用于挂接WordPress的核心功能,使您的主题更具模块化,可扩展性和功能性。 ## 什么是functions.php? functions.php文件的行为就像一个WordPress插件,向WordPress网站添加功能和功能。 您可以使用它来调用WordPress函数并定义自己的功能。 >[warning] 注意:使用插件或functions.php可以生成相同的结果。 如果您正在创建应该可用的新功能,无论网站如何,最好将其放入插件。 使用WordPress插件或使用functions.php有优势和折衷。 WordPress插件: - 需要具体的,唯一的标题文字; - 存储在wp-content/plugins中,通常在子目录中; - 激活时仅在页面加载时执行; - 适用于所有主题; 并且应该有一个目的 - 例如,提供搜索引擎优化功能或帮助备份。 同时,一个functions.php文件: - 不需要唯一的标题文字; - 存储在wp-content/themes中的主题的子目录中; - 仅在活动主题的目录中执行; - 仅适用于该主题(如果主题已更改,则不再使用该功能); 并且可以有许多代码块用于许多不同的目的。 - 每个主题都有自己的函数文件,但只有活动主题的functions.php中的代码才能实际运行。 如果你的主题已经有一个功能文件,你可以添加代码。 如果没有,您可以创建一个名为functions.php的纯文本文件,以添加到您的主题目录中,如下所述。 子主题可以有自己的functions.php文件。 将函数添加到子函数文件是修改父主题的无风险方式。 这样,当父主题更新时,您不必担心新添加的功能会消失。 >[warning] 注意:虽然子主题的functions.php是在父主题的functions.php之前由WordPress加载的,但它不会覆盖它。 子主题的functions.php可用于增加或替换父主题的功能。 同样,在加载任何插件文件之后,会加载functions.php。 使用functions.php可以: - 使用WordPress挂钩。 例如,使用excerpt_length过滤器,您可以更改您的职位摘录长度(默认为55个字)。 - 使用add_theme_support()启用WordPress功能。 例如,打开帖子缩略图,帖子格式和导航菜单。 - 定义要在多个主题模板文件中重用的功能。 >[warning] 警告:如果WordPress插件调用相同的功能或过滤器,就像您在functions.php中所做的那样,结果可能是意外的,甚至导致您的站点被禁用。 ## 示例 以下是您可以在functions.php文件中使用的一些示例,以支持各种功能。 如果您选择将其提交到WordPress.org主题目录,则这些示例中的每一个都可以在您的主题中使用。 ### 主题设置 一些主题功能应该包含在一个“设置”功能中,最初在您的主题被激活时运行。 如下图所示,这些功能可以添加到您的functions.php文件中,以激活推荐的WordPress功能。 >[warning] 注意:用你的主题名命名你的函数很重要。 以下所有示例都使用myfirsttheme_作为其命名空间,它们应根据您的主题名称进行自定义。 要创建此初始函数,请启动一个名为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() { ``` >[warning] 注意:在上面的例子中,函数myfirsttheme_setup启动但未关闭。 确保关闭您的功能 ### automatic-feed-links 默认情况下,自动Feed链接可以发布和评论RSS Feed。 这些Feed将自动显示在<head>中。 可以使用add_theme_support()调用它们。 ``` add_theme_support( 'automatic-feed-links' ); ``` ### 导航菜单 自定义导航菜单允许用户在“菜单”管理面板中编辑和自定义菜单,为用户提供了一个拖放界面来编辑其主题中的各种菜单。 您可以在functions.php中设置多个菜单。 可以使用register_nav_menus()添加它们,并使用wp_nav_menu()插入主题,如本手册后面所述。 如果您的主题将允许多个菜单,则应使用数组。 虽然某些主题将不具有自定义导航菜单,但建议您允许此功能轻松进行自定义。 ``` register_nav_menus( array( 'primary' => __( 'Primary Menu', 'myfirsttheme' ), 'secondary' => __( 'Secondary Menu', 'myfirsttheme' ) ) ); ``` 您可以稍后使用wp_nav_menu()并使用分配的名称(即主)将其定义的每个菜单作为theme_location参数。 ### 加载文本域 通过使您的主题中的字符串可用于翻译,主题可以翻译成多种语言。 为此,您必须使用load_theme_textdomain()。 有关使您的主题可用于翻译的更多信息,请阅读[国际化](themes/internationalization.md)部分。 ``` load_theme_textdomain( 'myfirsttheme', get_template_directory() . '/languages' ); ``` ### 发布缩略图 发布缩略图和精选图片可让您的用户选择一个图片来表示他们的帖子。 您的主题可以根据其设计决定如何显示它们。 例如,您可以选择在归档视图中显示每个帖子的帖子缩略图。 或者,您可能希望在主页上使用大型特色图片。 虽然不是每个主题都需要特色图片,但建议您支持发布缩略图和精选图片。 ``` add_theme_support( 'post-thumbnails' ); ``` ### 发布格式 发布格式允许用户以不同的方式格式化其帖子。 这对于允许博主根据帖子的内容选择不同的格式和模板非常有用。 add_theme_support()也用于Post格式。 这是推荐的。 ``` add_theme_support( 'post-formats', array ( 'aside', 'gallery', 'quote', 'image', 'video' ) ); ``` 进一步了解[发布格式](themes/post-formats.md)。 ### 初始设置示例 包括所有上述功能将给你一个如下所示的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; ``` ## 其他特性 还有其他常见功能可以在functions.php中包含。 下面列出了一些最常见的功能。 点击并了解有关这些功能的更多信息。 (根据新页面展开此部分。) - [自定义Headers](themes/custom-headers.md) - [Sidebars(Widgets)](themes/sidebars.md) - 自定义背景 - 添加编辑器样式 - 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' ); ```