the_taxonomies()
云策文档标注
概述
the_taxonomies() 函数用于显示文章的关联分类法,支持在循环内或循环外使用,并可自定义输出格式。
关键要点
- 该函数基于 get_the_taxonomies() 构建,用于输出分类法列表,无需指定 Post ID 即可在循环内使用。
- 参数 $args 为可选数组,包含 post、before、sep、after 等,用于控制输出内容和格式。
- 支持模板参数 template 和 term_template,允许自定义分类法标签和术语的显示方式。
- 函数内部使用 wp_parse_args() 合并参数,并调用 get_the_taxonomies() 获取数据后输出。
代码示例
// 使用数组参数调用
$args = array(
'before' => '<div class="taxonomies">',
'sep' => ', ',
'after' => '</div>',
);
the_taxonomies( $args );
// 用户贡献笔记中的示例
the_taxonomies( array(
'before' => '',
'template' => '%s: %l.',
'term_template' => '%2$s',
'sep' => '',
'after' => '',
) );注意事项
- 该函数直接输出内容,适用于主题开发中快速显示分类法,无需额外处理。
- 参数默认值基于 get_the_taxonomies(),确保兼容性和灵活性。
原文内容
Displays the taxonomies of a post with available options.
Description
This function can be used within the loop to display the taxonomies for a post without specifying the Post ID. You can also use it outside the Loop to display the taxonomies for a specific post.
Parameters
$argsarrayoptional-
Arguments about which post to use and how to format the output. Shares all of the arguments supported by get_the_taxonomies() , in addition to the following.
postint|WP_PostPost ID or object to get taxonomies of. Default current post.beforestringDisplays before the taxonomies.sepstringSeparates each taxonomy. Default is a space.afterstringDisplays after the taxonomies.
More Arguments from get_the_taxonomies( … $args )
Arguments about how to format the list of taxonomies.
templatestringTemplate for displaying a taxonomy label and list of terms.
Default is “Label: Terms.”term_templatestringTemplate for displaying a single term in the list. Default is the term name linked to its archive.
Default:
array()
Source
function the_taxonomies( $args = array() ) {
$defaults = array(
'post' => 0,
'before' => '',
'sep' => ' ',
'after' => '',
);
$parsed_args = wp_parse_args( $args, $defaults );
echo $parsed_args['before'] . implode( $parsed_args['sep'], get_the_taxonomies( $parsed_args['post'], $parsed_args ) ) . $parsed_args['after'];
}
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
Skip to note 3 content
Joy
This can be used in a theme to support all taxonomies easily.
<footer class="entry-footer"> '<div class="tax-link-wrap">', 'template' => '<span class="taxonomy-label">%s:</span> <span class="taxonomy-term-list">%l.</span>', 'term_template' => '<a href="%1$s" rel="tag">%2$s</a>', 'sep' => '<br />', 'after' => '</div>', ) ); ?> </footer>Skip to note 4 content
Codex
Example
// Arguments as a query-string. the_taxonomies( 'before=<ul>&after;=</ul>' ); '<p class="meta">', 'after' => '<p class="meta">', ); the_taxonomies( $args );