主题开发文档

💡 云策文档标注

概述

模板标签是 WordPress 主题中用于从数据库检索内容的代码片段,支持动态内容输出和模块化主题结构。它们通过 PHP 函数调用实现,可包含参数以定制数据获取和格式化。

关键要点

  • 模板标签由 PHP 代码标签、WordPress 函数和可选参数组成,用于调用主题文件或数据库信息。
  • 使用模板标签可提高主题维护性,如通过 get_header() 和 get_footer() 避免代码重复。
  • 模板标签能显示动态数据,例如使用 bloginfo('name') 自动获取站点标题,无需硬编码。
  • 部分模板标签需在 WordPress Loop 内使用,如 the_content() 和 the_excerpt(),而其他如 wp_list_cats() 则可在任意模板文件中使用。
  • 参数允许指定检索的数据类型和格式,例如 bloginfo() 可接受 'name' 或 'version' 参数。

代码示例

get_header();
bloginfo( 'name' );

注意事项

  • 确保在 Loop 内使用的模板标签(如 the_content())放置在正确位置,即 while ( have_posts() ) : the_post(); 之后和 endwhile; 之前。
  • 参考特定模板标签文档以了解参数详情和用法。

📄 原文内容

Template tags are used within themes to retrieve content from your database. The content could be anything from a blog title to a complete sidebar. Template tags are the preferred method to pull content into your theme because:

  • they can print dynamic content;
  • they can be used in multiple theme files; and
  • they separate the theme into smaller, more understandable, sections.

What is a Template Tag?

A template tag is simply a piece of code that tells WordPress to get something from the database. It is broken up into three components:

  • A PHP code tag
  • A WordPress function
  • Optional parameters

You can use a template tag to call another theme file or some information from the database.

For example, the template tag <a href="https://developer.wordpress.org/reference/functions/get_header/">get_header()</a> tells WordPress to get the header.php file and include it in the current theme file. Similarly, <a href="https://developer.wordpress.org/reference/functions/get_footer/">get_footer()</a> tells WordPress to get the footer.php file.

There are also other kinds of template tags:

  • <a href="https://developer.wordpress.org/reference/functions/the_title/">the_title()</a> – tells WordPress to get the title of the page or post from the database and include it.
  • <a href="https://developer.wordpress.org/reference/functions/bloginfo/">bloginfo( 'name' )</a> – tells WordPress to get the blog title out of the database and include it in the template file.

If you look closely at the last example, you will also see that there is a parameter between the parenthesis. Parameters let you do two things:

  1. ask for specific pieces of information and
  2. format the information in a certain way.

Parameters are covered extensively below, but it’s useful to be aware that you can send WordPress-specific instructions for how you want the data presented.

Why Use Template Tags

By encapsulating all of the code for a particular chunk of content, template tags make it very easy to include various pieces of a template in a theme file and also to maintain the theme.

It is far easier to create one header.php file and have all of your theme templates like single.php, page.php, front-page.php, etc. reference that one theme file using <a href="https://developer.wordpress.org/reference/functions/get_header/">get_header()</a> than copying and pasting the code into each theme file. It also makes maintenance easier. Whenever you make a change in your header.php file, the change is automatically carried over into all of your other theme files.

Another reason to use template tags is to display dynamic data, i.e. data from the database. In your header, you could manually include the title tag, like so:

<title>My Personal Website</title>

However, doing this means manually editing your theme any time you want to change the title of your website. Instead, it’s easier to include the <a href="https://developer.wordpress.org/reference/functions/bloginfo/">bloginfo( 'name' )</a> template tag, which automatically fetch the site title from the database. Now, you can change the title of your site in WordPress, rather than having to hard code your theme templates.

How to Use Template Tags

Using template tags is very simple. In any template file you can use a template tag by simply printing one line of php code to call the template tag. Printing the header.php file is as simple as:

get_header();

Parameters

Some template tags let you pass parameters. Parameters are extra pieces of information that determine what is retrieved from the database.

For example, the <a title="bloginfo template tag" href="https://developer.wordpress.org/reference/functions/bloginfo/">bloginfo()</a> template tag allows you to give it a parameter telling WordPress the specific piece of information you want. To print the blog name, you just pass along the parameter “name,” like so:

bloginfo( 'name' );

To print the version of WordPress that the blog is running on, you would pass a parameter of “version”:

bloginfo( 'version' );

For each template tag, the parameters differ. A list of the parameters and what they can do can be found on specific template tag pages located throughout the code reference.

Using Template Tags Within the Loop

Many template tags work within the WordPress Loop. This means that they are included in the template files as part of the php “loop” that generates the pages users see based upon the instructions inside of the loop.

The WordPress loop begins with:

if ( have_posts() ) :
	while ( have_posts() ) :
		the_post();

Template tags that work within the loop must be in the middle area, before the ending section of the loop below:

	endwhile;
else :
	_e( 'Sorry, no posts matched your criteria.', 'devhub' );
endif;

Some of template tags that need to be inside of the loop include

The main reason why some functions require the loop is because they require the global post object to be set.

If the template tag you want to use doesn’t have to be within the loop

then you can put it in any file you’d like, for instance in the sidebar, header, or footer template files.

These are functions that typically do not require the global post object.

See Also