WordPress模板标签完全清单:主题开发者的速查手册
WordPress模板标签,本质上就是一些预置的PHP函数,专门用来在主题模板中动态获取和展示内容。比如你想显示文章标题,不需要写复杂的数据库查询,直接丢一个the_title()就搞定了。这些标签文件统一放在wp-includes目录里,名字都以-template.php结尾。本文梳理了9个核心标签文件涵盖的所有标签,按功能分组说明,方便初学者快速查阅和理解。
模板标签的命名规则
使用模板标签前,先搞懂它的命名套路。
WordPress的模板标签通常遵循一对规则:输出型和获取型。
the_开头:直接输出内容。比如the_title(),调用后标题就显示在页面上了。get_开头:返回内容,让你自己处理。比如get_the_title(),你可以把标题赋值给变量,加工后再输出。
php
// 直接输出 the_title(); // 获取后自定义包装 $title = get_the_title(); echo '<h2 class="my-title">' . $title . '</h2>';
注意,大多数内容相关标签(如the_title、the_content)必须在WordPress主循环(The Loop)内部使用,才能正确获取当前文章的数据。而像bloginfo()这类站点信息标签,可以在模板任何位置使用。
一、通用模板标签
这类标签控制主题的结构骨架,位于wp-includes/general-template.php。
引入模板文件:
| 标签 | 作用 |
|---|---|
get_header() | 引入header.php头部文件 |
get_footer() | 引入footer.php底部文件 |
get_sidebar() | 引入sidebar.php侧边栏文件 |
get_template_part() | 引入任意模板片段,比如get_template_part('loop')会引入loop.php |
get_search_form() | 显示搜索表单 |
站点信息:
| 标签 | 作用 |
|---|---|
bloginfo('name') | 站点标题 |
bloginfo('description') | 站点副标题 |
bloginfo('url') | 站点地址 |
bloginfo('version') | WordPress版本号 |
get_current_blog_id() | 当前站点ID(多站点用) |
登录与注册:
| 标签 | 作用 |
|---|---|
wp_loginout() | 显示登录/登出链接 |
wp_login_url() | 获取登录页面地址 |
wp_logout_url() | 获取登出地址 |
wp_lostpassword_url() | 获取找回密码地址 |
wp_login_form() | 显示登录表单 |
wp_register() | 显示注册链接 |
页面标题:
| 标签 | 作用 |
|---|---|
wp_title() | 页面标题 |
single_post_title() | 单篇文章的标题 |
post_type_archive_title() | 自定义文章类型存档页标题 |
single_cat_title() | 分类存档页标题 |
single_tag_title() | 标签存档页标题 |
single_term_title() | 自定义分类法存档页标题 |
存档相关:
| 标签 | 作用 |
|---|---|
wp_get_archives() | 显示日期存档列表 |
get_calendar() | 显示日历式存档 |
资源加载:
| 标签 | 作用 |
|---|---|
wp_enqueue_script() | 安全加载JavaScript文件 |
wp_enqueue_style() | 安全加载CSS文件(本文件未列出,但实际存在) |
二、作者标签
位于wp-includes/author-template.php,用于显示作者相关信息。
| 标签 | 作用 |
|---|---|
the_author() | 显示作者名称 |
get_the_author() | 获取作者名称 |
the_author_link() | 带链接的作者名 |
the_author_meta() | 显示作者元数据(邮箱、简介等) |
the_author_posts() | 该作者发表的文章数量 |
the_author_posts_link() | 链接到该作者所有文章的存档页 |
wp_list_authors() | 列出所有作者 |
wp_dropdown_users() | 作者下拉列表 |
get_author_posts_url() | 获取作者存档页地址 |
php
// 显示当前文章作者的名字,并链接到他的所有文章 <?php the_author_posts_link(); ?> // 输出效果:<a href="作者存档地址">作者名</a>
三、分类与标签标签
位于wp-includes/category-template.php,处理分类目录、标签和自定义分类法。
| 标签 | 作用 |
|---|---|
the_category() | 显示文章所属分类(带链接) |
the_category_rss() | RSS中显示分类 |
single_cat_title() | 显示当前分类存档页的名称 |
wp_list_categories() | 列出所有分类 |
wp_dropdown_categories() | 分类下拉列表 |
single_tag_title() | 显示当前标签存档页的名称 |
the_tags() | 显示文章关联的标签 |
wp_tag_cloud() | 显示标签云 |
wp_generate_tag_cloud() | 生成标签云数据 |
tag_description() | 获取标签描述 |
category_description() | 获取分类描述 |
term_description() | 获取任意分类法项目的描述 |
the_terms() | 显示文章关联的任意分类法项目 |
the_taxonomies() | 显示文章的所有分类法 |
php
// 显示当前文章的分类,用逗号隔开
<?php the_category(', '); ?>
// 输出:WordPress, 主题开发, 教程
// 显示标签云
<?php wp_tag_cloud('smallest=12&largest=20&number=30'); ?>
四、链接与固定链接标签
位于wp-includes/link-template.php,处理各种URL。
文章链接:
| 标签 | 作用 |
|---|---|
the_permalink() | 显示当前文章的链接 |
get_permalink() | 获取当前文章的链接(返回,不输出) |
get_the_permalink() | 同上,是get_permalink的别名 |
get_page_link() | 获取页面链接 |
get_attachment_link() | 获取附件链接 |
get_post_permalink() | 获取自定义文章类型链接 |
站点链接:
| 标签 | 作用 |
|---|---|
home_url() | 获取网站首页地址 |
site_url() | 获取WordPress安装地址 |
get_home_url() | 获取首页地址(返回) |
get_admin_url() | 获取后台地址 |
搜索与Feed:
| 标签 | 作用 |
|---|---|
get_search_link() | 获取搜索页地址 |
get_search_query() | 获取当前搜索关键词 |
the_feed_link() | 获取Feed地址 |
the_privacy_policy_link() | 显示隐私政策页面链接 |
编辑与删除链接:
| 标签 | 作用 |
|---|---|
edit_post_link() | 显示编辑文章链接(仅登录且有权限用户可见) |
get_edit_post_link() | 获取编辑文章链接地址 |
get_delete_post_link() | 获取删除文章链接地址 |
edit_comment_link() | 显示编辑评论链接 |
edit_bookmark_link() | 显示编辑书签链接 |
edit_tag_link() | 显示编辑标签链接 |
五、文章标签
位于wp-includes/post-template.php,常用的标签组。
文章内容:
| 标签 | 作用 |
|---|---|
the_title() | 显示文章标题 |
get_the_title() | 获取文章标题 |
the_title_attribute() | 安全地输出标题(用于HTML属性) |
the_content() | 显示文章正文(带格式处理) |
the_excerpt() | 显示文章摘要 |
the_ID() | 显示文章ID |
post_class() | 输出文章容器的CSS类 |
body_class() | 输出<body>标签的CSS类 |
导航链接:
| 标签 | 作用 |
|---|---|
next_post_link() | 下一篇帖子链接 |
previous_post_link() | 上一篇帖子链接 |
next_posts_link() | 下一页链接(列表页) |
previous_posts_link() | 上一页链接(列表页) |
posts_nav_link() | 上下页导航 |
wp_link_pages() | 多页文章的分页导航 |
日期时间:
| 标签 | 作用 |
|---|---|
the_date() | 显示文章发布日期 |
the_time() | 显示文章发布时间 |
get_the_date() | 获取发布日期 |
the_modified_date() | 显示修改日期 |
the_modified_time() | 显示修改时间 |
the_date_xml() | 以XML格式输出日期 |
single_month_title() | 月度存档标题 |
附件相关:
| 标签 | 作用 |
|---|---|
wp_get_attachment_link() | 获取附件完整HTML链接 |
wp_get_attachment_image() | 获取附件图片HTML |
wp_get_attachment_image_src() | 获取附件图片URL等信息 |
wp_attachment_is_image() | 判断附件是否为图片 |
短链接:
| 标签 | 作用 |
|---|---|
wp_get_shortlink() | 获取文章短链接 |
the_shortlink() | 显示短链接HTML |
php
// 标准文章循环示例
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
<div class="entry-meta">
发布于: <?php the_time('Y年m月d日'); ?>
</div>
<div class="entry-content">
<?php the_content(); ?>
</div>
</article>
<?php endwhile; endif; ?>
六、文章缩略图标签
位于wp-includes/post-thumbnail-template.php,处理特色图像(Featured Image)功能。注意,该功能需要在functions.php中先用add_theme_support('post-thumbnails')开启。
| 标签 | 作用 |
|---|---|
has_post_thumbnail() | 判断当前文章是否有特色图像 |
get_post_thumbnail_id() | 获取特色图像的附件ID |
the_post_thumbnail() | 直接输出特色图像HTML |
get_the_post_thumbnail() | 获取特色图像HTML(返回) |
php
// 在循环中判断并显示特色图像
<?php if (has_post_thumbnail()) : ?>
<div class="post-thumb">
<?php the_post_thumbnail('medium'); ?>
</div>
<?php endif; ?>
七、评论标签
位于wp-includes/comment-template.php,处理评论显示。
| 标签 | 作用 |
|---|---|
wp_list_comments() | 列出所有评论 |
comment_form() | 显示评论表单 |
comments_template() | 引入评论模板(本文件未列出但实际存在) |
comments_number() | 显示评论数量 |
comments_popup_link() | 弹窗方式显示评论链接 |
paginate_comments_links() | 评论分页导航 |
get_avatar() | 获取用户头像 |
comment_author() | 评论作者名 |
comment_author_link() | 带链接的评论作者名 |
comment_author_email() | 评论作者邮箱 |
comment_author_url() | 评论作者网站地址 |
comment_date() | 评论日期 |
comment_time() | 评论时间 |
comment_text() | 评论正文 |
comment_excerpt() | 评论摘要 |
comment_ID() | 评论ID |
comment_class() | 评论容器的CSS类 |
comment_reply_link() | 回复链接 |
cancel_comment_reply_link() | 取消回复链接 |
next_comments_link() | 下一页评论链接 |
previous_comments_link() | 上一页评论链接 |
八、书签/链接标签
位于wp-includes/bookmark-template.php,管理友情链接(Blogroll)。该功能在WordPress 3.5+后默认隐藏,可通过插件重新启用。
| 标签 | 作用 |
|---|---|
wp_list_bookmarks() | 列出所有书签/友情链接 |
get_bookmark() | 获取单个书签数据 |
get_bookmarks() | 获取所有书签 |
get_bookmark_field() | 获取书签特定字段 |
九、导航菜单标签
位于wp-includes/nav-menu-template.php,处理自定义菜单。
| 标签 | 作用 |
|---|---|
wp_nav_menu() | 显示自定义导航菜单 |
walk_nav_menu_tree() | 递归遍历菜单树(核心函数,一般不用直接调用) |
注意,自定义菜单需要先在functions.php中注册菜单位置:
php
// functions.php
register_nav_menus(array(
'primary' => '主导航',
'footer' => '底部导航'
));
// 模板文件中调用
<?php wp_nav_menu(array('theme_location' => 'primary')); ?>
总结
模板标签是WordPress主题开发的基础工具。理解这些标签的用途和适用场景,能让你更地搭建主题。核心要点:
the_输出,get_获取。两者分工明确,根据需要选用。- 大多数内容标签必须放在循环(The Loop)里才能正常工作。
- 站点信息类标签(如
bloginfo)不受循环限制,随处可用。 - 参数决定标签的行为——多翻官方文档,熟能生巧。