主题开发文档

链接主题文件与目录

💡 云策文档标注

概述

本文档介绍了在 WordPress 主题开发中如何链接核心主题文件(如 header.php、footer.php、sidebar.php)和自定义模板文件,以及如何动态链接主题目录和页面。重点包括使用 Template Tags 和 get_template_part() 函数来组织和调用模板文件。

关键要点

  • 使用 get_header()、get_footer()、get_sidebar() 等 Template Tags 调用标准主题文件,支持自定义版本如 header-{custom}.php。
  • 通过 get_template_part() 函数加载自定义模板文件,支持文件命名约定如 content-product.php 和目录组织。
  • 使用 get_theme_file_uri() 和 get_theme_file_path() 函数链接主题目录中的文件,这些函数在子主题中优先检查子主题文件。
  • 在子主题中,可使用 get_parent_theme_file_uri() 和 get_parent_theme_file_path() 直接链接父主题文件。
  • 动态链接页面或文章可使用 get_permalink($ID),基于 ID 而非 slug,避免链接断裂但可能增加数据库查询。

代码示例

get_header('your_custom_template');
get_footer('your_custom_template');
get_sidebar('your_custom_template');

get_template_part('content', 'product');
get_template_part('content-templates/content', 'location');

echo get_theme_file_uri('images/logo.png');
echo get_parent_theme_file_uri('images/logo.png');

<a href="<?php echo get_permalink($ID); ?>">This is a link</a>

注意事项

  • get_theme_file_uri()、get_theme_file_path()、get_parent_theme_file_uri()、get_parent_theme_file_path() 函数从 WordPress 4.7 开始引入,旧版本需使用 get_template_directory_uri() 等函数。
  • 这些函数返回文件 URI 或路径时,无论文件是否存在,都可能返回损坏链接,需确保文件存在。
  • 动态链接使用 get_permalink($ID) 时,虽然 ID 稳定,但可能增加数据库查询,需权衡性能。

📄 原文内容

Linking to Core Theme Files

As you’ve learned, WordPress themes are built from a number of different template files. At the very least this will usually include a sidebar.php, header.php and footer.php. These are called using Template Tags, for example:

You can create custom versions of these files can be called as well by naming the file sidebar-{your_custom_template}.php, header-{your_custom_template}.php and footer-{your_custom_template}.php. You can then use Template Tags with the custom template name as the only parameter, like this:

get_header( 'your_custom_template' );
get_footer( 'your_custom_template' );
get_sidebar( 'your_custom_template' );

WordPress creates pages by assembling various files. Aside from the standard files for the header, footer and sidebar, you can create custom template files and call them at any location in the page using get_template_part() . To create a custom template file in your theme give the file an appropriate name and use the same custom template system as with the header, sidebar and footer files:

slug-template.php

For example, if you would like to create a custom template to handle your post content you could create a template file called content.php and then add a specific content layout for product content by extending the file name to content-product.php. You would then load this template file in your theme like this:

get_template_part( 'content', 'product' );

If you want to add more organization to your templates, you can place them in their own directories within your theme directory. For example, suppose you add a couple more content templates for profiles and locations, and group them in their own directory called content-templates.

The theme hierarchy for your theme called my-theme might look like the following. style.css and page.php are included for context.

  • themes
  • my-theme
  • content-templates
  • content-location.php
  • content-product.php
  • content-profile.php
  • style.css

To include your content templates, prepend the directory names to the slug argument like this:

get_template_part( 'content-templates/content', 'location' );
get_template_part( 'content-templates/content', 'product' );
get_template_part( 'content-templates/content', 'profile' );

Linking to Theme Directories

To link to the theme’s directory, you can use the following function:

If you are not using a child theme, this function will return the full URI to your theme’s main folder. You can use this to reference sub-folders and files in your theme like this:

echo get_theme_file_uri( 'images/logo.png' );

If you are using a child theme then this function will return the URI of the file in your child theme if it exists. If the file cannot be found in your child theme, the function will return the URI of the file in the parent theme. This is particularly important to keep in mind when distributing a theme or in any other case where a child theme may or may not be active.

To access the path to a file in your theme’s directories, you can use the following function:

Like get_theme_file_uri() , this will access the path of the file in the child theme if it exists. If the file cannot be found in the child theme, the function will access the path to the file in the parent theme.

In a child theme, you can link to a file URI or path in the parent theme’s directories using the following functions:

As with  get_theme_file_uri(), you can reference sub-folders and files like this:

echo get_parent_theme_file_uri( 'images/logo.png' );
//or
echo get_parent_theme_file_path( 'images/logo.png' );

Take care when referencing files that may not be present, as these functions will return the URI or file path whether the file exists or not. If the file is missing, these functions will return a broken link.

The functions get_theme_file_uri() , get_theme_file_path() , get_parent_theme_file_uri() , get_parent_theme_file_path() were introduced in WordPress 4.7.

For previous WordPress versions, use get_template_directory_uri() , get_template_directory() , get_stylesheet_directory_uri() , get_stylesheet_directory() .

Take note that the newer 4.7 functions run the older functions anyway as part of the checking process so it makes sense to use the newer functions when possible.

Dynamic Linking in Templates

Regardless of your permalink settings, you can link to a page or post dynamically by referring to its unique numerical ID (seen in several pages in the admin interface) with

<a href="<?php echo get_permalink($ID); ?>">This is a link</a>

This is a convenient way to create page menus as you can later change page slugs without breaking links, as IDs will stay the same. However, this might increase database queries.