在本系列的第二部分中,我们介绍了第一批WordPress模板标签。 在第三部分中,我们将介绍第二批近200个模板标签。 在整个教程中,我们将看到有关分类法和术语的模板标签。
获取类别的描述: category_description()
此模板标记返回类别的描述。
参量
此模板标记仅接受一个参数:
-
$category_ID
(可选-整数) :
类别的ID。
(默认值:查询的类别)
用法
<?php
$cat_desc = get_category_description( 15 );
?>
获取标签的描述: tag_description()
此模板标签返回标签的描述。
参量
此模板标记仅接受一个参数:
-
$tag_ID
(可选-整数) :
标签的ID。
(默认:当前标签)
用法
<?php
// Display the description of current tag.
echo tag_description();
// Get the description of a specific tag.
$special_tag_desc = tag_description( 42 );
?>
获取术语的描述: term_description()
该模板标记仅返回术语的描述。
参量
此模板标记接受两个参数:
-
$term_ID
(可选-整数) :
术语的ID。
(默认:当前术语) -
$taxonomy
(可选-字符串) :
术语的分类法。
(默认值:“ post_tag”)
用法
<?php
// Display the description of current term.
echo term_description();
// Get the description of a specific term from a specific taxonomy.
$special_term_desc = term_description( 14, 'laptop_inches' );
?>
获取和显示类别档案的页面标题: single_cat_title()
此模板标记返回并显示类别标题以供在页面标题中使用。
参量
此模板标记接受两个参数:
-
$prefix
(可选-字符串) :
标题的前缀。
(默认:空) -
$echo
(可选-布尔值) :
是回显(TRUE
)还是返回(FALSE
)标签。
(默认:TRUE
)
用法
<?php
// Display the category title with a prefix.
single_cat_title( 'Category: ' );
// Get the category title.
$cat_title = single_cat_title( '', false );
?>
获取和显示标签档案的页面标题: single_tag_title()
该模板标签返回并显示标签标题,以供在页面标题中使用。
参量
此模板标记接受两个参数:
-
$prefix
(可选-字符串) :
标题的前缀。
(默认:空) -
$echo
(可选-布尔值) :
是回显(TRUE
)还是返回(FALSE
)标签。
(默认:TRUE
)
用法
<?php
// Display the tag title with a prefix.
single_tag_title( 'Tag Archive: ' );
// Get the tag title.
$tag_title = single_tag_title( '', false );
?>
获取和显示术语存档的页面标题: single_term_title()
该模板标记返回并显示要在页面标题中使用的术语标题。
参量
此模板标记接受两个参数:
-
$prefix
(可选-字符串) :
标题的前缀。
(默认:空) -
$echo
(可选-布尔值) :
是回显(TRUE
)还是返回(FALSE
)标签。
(默认:TRUE
)
用法
<?php
// Display the term title with a prefix.
single_term_title( 'Term Archive: ' );
// Get the term title.
$term_title = single_term_title( '', false );
?>
获取并显示帖子的类别: get_the_category_list()
和the_category()
这些模板标签获取并输出分配给帖子的类别。
参量
这两个模板标签都接受三个参数:
-
$separator
(可选-字符串) :
分隔符文本。
(默认值:空-如果此参数为空,则函数将返回并显示无序的类别列表) -
$parents
(可选-字符串) :
如何显示父类别(“多个”,“单个”或空)。
(默认:空) -
$post_ID
(可选-整数) :
帖子的ID。
(默认:当前帖子)
用法
<?php
// Get an unordered list of current post's categories.
get_the_category_list();
// Display current post's categories separated by commas and with their parents (if there are any).
the_category( ', ', 'multiple' );
?>
获取和显示供稿帖子的类别: get_the_category_rss()
和the_category_rss()
这些模板标记使分配给帖子的类别可用于提要并返回或显示。
参量
这两个模板标记仅接受一个参数:
-
$feed_type
(可选-字符串) :
提要的类型。
(默认:默认供稿类型)
用法
<?php
// Get feed items of categories for the default feed type.
get_the_category_rss();
// Display feed items of categories for atom feeds.
the_category_rss( 'atom' );
?>
获取和显示帖子的标签: get_the_tag_list()
和the_tags()
这些模板标签返回或显示您的帖子标签。
参量
这两个模板标签都接受三个参数:
-
$before
(可选-字符串) :
在输出之前显示的文本或HTML代码。
(默认值:空为get_the_tag_list()
和'标签'用于the_tags()
-
$sep
(可选-字符串) :
用作分隔符的文本。
(默认:对于get_the_tag_list()
为空,对于'the_tags()
为',') -
$after
(可选-字符串) :
输出后要显示的文本或HTML代码。
(默认:空)
用法
<?php
// Get a plain list of tag links.
get_the_tag_list();
// Display the tags with custom parameters.
the_tags( '<div class="mytheme-tags">' . __( 'Tags: ', 'translation-domain' ) , ' · ', '</div>' );
?>
获取和显示帖子的条款: get_the_term_list()
和the_terms()
这些模板标记获取并回显为该帖子分配的术语列表。
参量
这两个模板标签都接受五个参数:
-
$post_ID
(可选-整数) :
帖子的ID。
(默认:当前帖子) -
$taxonomy
(必需,字符串) :
分类法以显示其术语。
(默认:空) -
$before
(可选-字符串) :
在输出之前显示的文本或HTML代码。
(默认:空) -
$sep
(可选-字符串) :
分隔符文本。
(默认:',') -
$after
(可选-字符串) :
输出后要显示的文本或HTML代码。
(默认:空)
用法
<?php
// Getting current post's terms from the "Available Brands" taxonomy.
get_the_term_list( $post->ID, 'available-brands' );
// Display current post's terms from the "Available Brands" taxonomy with some customization.
the_terms( $post->ID, 'available-brands', __( 'Available Brands: ', 'translation-domain' ), ' - ' );
?>
显示帖子的分类法: the_taxonomies()
此模板标签显示分类法和与这些分类法关联的术语。
参量
此模板标记仅接受一个参数:
-
$args
(可选-数组) :
以下参数的数组:-
'post'
(整数):帖子ID。
(默认值:0) -
'before'
(字符串):在输出之前添加HTML代码。
(默认:空) -
'sep'
(字符串):分隔符文本。
(默认:“”) -
'after'
(字符串):在输出之后添加HTML代码。
(默认:空) -
'template'
(字符串):用于显示分类术语的模板。
(默认:'%s:%l。',其中%s
是分类标签,%t
是术语链接列表)
-
用法
<?php
$args = array(
'before' => '<div class="taxonomies">',
'sep' => ', ',
'after' => '</div>'
);
the_taxonomies( $args );
?>
获取并显示“编辑标签”链接: get_edit_tag_link()
和edit_tag_link()
这些模板标签返回或回显“编辑此标签”链接,供您在模板中使用。
参量
get_edit_tag_link()
接受两个参数:
-
$tag_ID
(必需-整数) :
标签的ID。
(默认值:NULL
) -
$taxonomy
(可选-字符串) :
分类的类型。
(默认值:“ post_tag”)
并且edit_tag_link()
接受四个参数:
-
$link
(可选-字符串) :
链接显示的文本。
(默认:“编辑此”) -
$before
(可选-字符串) :
在输出之前显示的文本或HTML代码。
(默认:空) -
$after
(可选-字符串) :
输出后要显示的文本或HTML代码。
(默认:空) -
$tag
(必填,对象) :
标记对象。
(默认值:NULL
)
用法
<?php
// Retrieving the current tag's "edit tag" link.
$edit_tag_link = get_edit_tag_link();
// Retrieving a specific term's "edit term" link.
$edit_tag_link = get_edit_tag_link( 4, 'keyboard-brand' );
// Displaying the current tag's "edit tag" link.
edit_tag_link( __( 'Edit', 'translation-domain' ), '<strong>', '</strong>' );
// Displaying a specific tag's "edit tag" link.
edit_tag_link( __( 'Edit', 'translation-domain' ), '', '', 11 );
?>
获取和显示类别列表: wp_list_categories()
此模板标记返回或显示任何种类的分类中的类别或术语的列表。
参量
此模板标记仅接受一个参数:
-
$args
(可选-数组) :
以下参数的数组:-
'show_option_all'
(字符串):如果将其设置为非空白值,则显示指向主页的链接(或在“设置”>“阅读 ”中设置的“帖子”页面)。 该值将是链接的锚文本。
(默认:空) -
'show_option_none'
(字符串):用于显示是否没有任何类别的文本。
(默认:“无类别”) -
'orderby'
(字符串):如何排序链接。 接受“名称”,“ ID”,“子弹”,“计数”和“ term_group”。
(默认:“名称”) -
'order'
(字符串):是以升序('ASC')还是降序('DESC')排序商品。
(默认值:“ ASC”) -
'style'
(字符串):显示类别的样式-'list'生成无序列表,或'none'生成用<br />
分隔的链接列表。
(默认:“列表”) -
'show_count'
(整数):在每个类别链接旁边显示帖子数。
(默认值:0) -
'hide_empty'
(整数):是否隐藏空类别。
(默认值:1) -
'use_desc_for_title'
(整数):使用描述作为链接的“ title”属性。
(默认值:1) -
'child_of'
(整数):仅显示类别的给定ID的子代。
(默认值:0) -
'feed'
(字符串):如果设置为任何非空白值,则类别供稿链接将显示在类别链接旁边。 该值将是链接的锚文本。
(默认:空) -
'feed_type'
(字符串):提要的类型('rss','rss2','atom'或'rdf')。
(默认:空) -
'feed_image'
(字符串):覆盖锚文本并显示图像的图像路径。
(默认:空) -
'exclude'
(字符串):以逗号分隔的要排除的类别ID列表。
(默认:空) -
'exclude_tree'
(字符串):与“ exclude”相同,但是如果“ hierarchical”参数设置为1,则必须使用此值
(默认:空) -
'current_category'
(整数):适当时添加“ current-cat”类。
(默认值:0) -
'hierarchical'
(整数):显示项目是否具有等级顺序。
(默认值:1) -
'title_li'
(字符串):使用<li>
标记包装输出,并在类别列表之前显示标题。 您可能希望将此设置为空字符串并禁用换行。
(默认:“类别”) -
'echo'
(整数):是回显输出(1)还是返回输出(0)。
(默认值:1) -
'depth'
(整数):最大深度。
(默认值:0) -
'taxonomy'
(字符串):要列出的分类法。
(默认:“类别”)
-
用法
<?php
$args = array(
// Show an "All Posts" link.
'show_option_all' => __( 'All Posts', 'translation-domain' ),
// Order by post count.
'orderby' => 'count',
// Show post count.
'show_count' => 1,
// Show feed links.
'feed' => 'Feed',
// Remove title_li.
'title_li' => ''
);
wp_list_categories( $args );
?>
获取和显示类别HTML下拉列表: wp_dropdown_categories()
该模板标记返回或回显您选择的分类法中类别或术语的下拉菜单( <select>
)。
参量
此模板标记仅接受一个参数:
-
$args
(可选-数组) :
以下参数的数组:-
'show_option_all'
(字符串):显示的文本,用于显示所有类别/分类法。
(默认:空) -
'show_option_none'
(字符串):要显示的文本,不显示类别/分类法。
(默认:空) -
'option_none_value'
(混合):未选择类别/分类法时要使用的值。
(默认值:-1) -
'orderby'
(字符串):用于排序类别/分类法的列。
(默认:“ ID”) -
'order'
(字符串):排序类别/分类法的方向。
(默认值:“ ASC”) -
'show_count'
(布尔值或整数):是否显示类别/分类法中的帖子数。
(默认值:0) -
'hide_empty'
(布尔值或整数):是否隐藏没有附加任何帖子的类别/分类法。
(默认值:1) -
'child_of'
(整数):显示作为给定ID的后代的所有类别。
(默认值:0) -
'exclude'
(字符串):逗号分隔的类别/分类ID列表,从列表中排除。
(默认:空) -
'echo'
(布尔或整数):是显示还是检索内容。
(默认值:1) -
'depth'
(整数):最大深度。
(默认值:0) -
'tab_index'
(整数):SELECT
元素的标签索引。
(默认值:0) -
'name'
(字符串):SELECT
元素的NAME
属性值。
(默认:“猫”) -
'id'
(字符串):SELECT
元素的ID。
(默认:空) -
'class'
(字符串):SELECT
元素CSS类。
(默认:“ postform”) -
'selected'
(整数):默认情况下要选择的类别/分类ID。
(默认值:0) -
'taxonomy'
(字符串):要检索的分类法的名称。
(默认:“类别”)
-
用法
<?php
$args = array(
'show_option_none' => __( 'Nothing!', 'translation-domain' ),
'orderby' => 'title',
'show_count' => 1,
'echo' => 0,
'name' => __( 'brands', 'translation-domain' ),
'taxonomy' => __( 'available-brands', 'translation-domain' )
);
$brands_dropdown = wp_categories_dropdown( $args );
?>
获取和显示标签云: wp_tag_cloud()
此模板标签返回或显示根据您选择的分类法生成的术语云(默认情况下为标签)。
参量
此模板标记仅接受一个参数:
-
$args
(可选-数组) :
以下参数的数组:-
'smallest'
(整数):最小的文字大小。
(默认值:8) -
'largest'
(整数):最大的文本大小。
(预设值:22) -
'unit'
(字符串):文本大小单位。
(默认值:“ pt”) -
'number'
(字符串):要返回多少标签
(默认值:45) -
'format'
(字符串):如何返回标签列表-“ flat”表示以空格分隔的扁平列表,“ list”表示HTML无序列表,“ array”表示PHP数组。
(预设值:'flat') -
'separator'
(字符串):分隔符文本。
(默认:“ \ n”) -
'orderby'
(字符串):如何按('名称'或'计数')对标签进行排序。
(默认:“名称”) -
'order'
(字符串):是以升序('ASC')还是降序('DESC')排序商品。
(默认值:“ ASC”) -
'include'
(数组):要包含的ID的数组。
(默认:空) -
'exclude'
(数组):要排除的ID数组。
(默认:空) -
'taxonomy'
(字符串):从中获取术语的分类法。
(默认值:“ post_tag”) -
'link'
(字符串):'view'用于前端查看,'edit'用于后端编辑链接。 与'post_type'参数一起使用。
(默认视图') -
'post_type'
(字符串):如果将“ link”参数设置为“ edit”,则必须将“ post_type”设置为帖子类型,以设置将在“ edit links”列表中列出的帖子类型。
(默认:空) -
'echo'
(布尔值):是否回显模板标签。
(默认:TRUE
)
-
用法
<?php
$args = array(
// Smallest & largest font sizes.
'smallest' => 10,
'largest' => 32,
// Get only 20 tags.
'number' => 20,
// Order by term popularity.
'orderby' => 'count',
// Custom taxonomy.
'taxonomy' => 'brands',
// Return output instead of echoing.
'echo' => 0
);
$tag_cloud = wp_tag_cloud( $args );
?>
获取自定义生成的标签云: wp_generate_tag_cloud()
该模板标签返回标签云,但是这次您可以选择自己的标签。
参量
此模板标记接受两个参数:
-
$tags
(必填-数组) :
标签列表的数组。
(默认值:NULL
) -
$args
(可选-数组) :
以下参数的数组:-
'smallest'
(整数):最小的文字大小。
(默认值:8) -
'largest'
(整数):最大的文本大小。
(预设值:22) -
'unit'
(字符串):文本大小单位。
(默认值:“ pt”) -
'number'
(字符串):要返回多少标签
(默认值:0) -
'format'
(字符串):如何返回标签列表-“ flat”表示以空格分隔的扁平列表,“ list”表示HTML无序列表,“ array”表示PHP数组。
(预设值:'flat') -
'separator'
(字符串):分隔符文本。
(默认:“ \ n”) -
'orderby'
(字符串):如何按('名称'或'计数')对标签进行排序。
(默认:“名称”) -
'order'
(字符串):是以升序('ASC')还是降序('DESC')排序商品。
(默认值:“ ASC”) -
'topic_count_text'
(字符串):这是来自_n_noop()的nooped复数形式,用于为标签链接的工具提示生成文本。
(默认值:NULL
) -
'topic_count_text_callback'
(字符串):这是回调函数,它提供带有该标签的帖子数,并返回标签链接工具提示的文本。
(默认值:NULL
) -
'topic_count_scale_callback'
(字符串):这是回调函数,与'smallest'
和'largest'
参数一起使用,以计算生成的标签的字体大小。
(默认:“ default_topic_count_scale”) -
'filter'
(整数):如果将其设置为0,则该函数将不传递具有相同名称的过滤器。
(默认值:1)
-
用法
<?php
// Get an array of the tags from current post.
$tags = get_the_tags( '', ', ', '' );
// Set arguments.
$args = array(
'smallest' => 10,
'largest' => 36,
'orderby' => 'count'
);
wp_generate_tag_cloud( $tags, $args );
?>
结论
希望您喜欢第二批模板标签。 还有六批次,敬请期待更多模板标签!
如果您有任何疑问,评论或更正,可以在“评论”部分与我们分享您的想法。 而且,如果您喜欢这篇文章,请不要忘记与您的朋友分享!
翻译自: https://code.tutsplus.com/tutorials/the-tuts-guide-to-template-tags-second-batch--cms-22576