2023-08-31

模板标签:Tuts+ 指南继续

模板标签:Tuts+ 指南继续

在本系列的第二部分中,我们介绍了第一批 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 (可选 – 布尔值)

    是否 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 (可选 – 布尔值)

    是否 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 (可选 – 布尔值)

    是否 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' ) , ' &middot; ', '</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 (可选—字符串)

    链接显示的文本。

    (默认:“编辑此”)< /em>

  • $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'(字符串):如何对链接进行排序。接受“name”、“ID”、“slug”、“count”和“term_group”。

      (默认:“name”)

    • 'order'(字符串):是否按升序(’ASC’)或降序(’DESC’)排序项目。

      (默认:’ASC’)

    • 'style'(字符串):显示类别的样式 – ‘list’ 生成无序列表或 ‘none’ 生成以
      s 分隔的链接列表。

      (默认:’列表’)

    • 'show_count'(整数):在每个类别链接旁边显示帖子计数。

      (默认值:0)

    • 'hide_empty'(整数):是否隐藏空类别。

      (默认:1)

    • 'use_desc_for_title'(整数):使用描述作为链接的“标题”属性。

      (默认:1)

    • 'child_of'(整数):仅显示类别给定 ID 的子级。

      (默认值:0)

    • 'feed'(字符串):如果设置为任何非空值,类别提要链接将出现在类别链接旁边。该值将是链接的锚文本。

      (默认:空)

    • 'feed_type'(字符串):提要类型(’rss’、’rss2’、’atom’ 或 ‘rdf’)。

      (默认:空)

    • 'feed_image'(字符串):覆盖锚文本并显示图像的图像路径。

      (默认:空)

    • 'exclude'(字符串):要排除的以逗号分隔的类别 ID 列表。

      (默认:空)

    • 'exclude_tree' (string): 与“exclude”相同,但如果“hierarchical”参数设置为 1,则必须使用这个

      (默认:空)

    • 'current_category'(整数):在合适的情况下添加“current-cat”类。

      (默认:0)

    • 'hierarchical'(整数):是否显示具有分层顺序的项目。

      (默认:1)

    • 'title_li'(字符串):使用

    • 标签包装输出,并在类别列表之前显示标题。您可能希望将其设置为空字符串并禁用换行。

      (默认:“类别”)

    • 'echo'(整数):是否回显输出(1)或返回它(0)。

      (默认:1)

    • 'depth'(整数):最大深度。

      (默认:0)

    • 'taxonomy'(字符串):要列出的分类法。

      (默认:’category’)

用法

<?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()

此模板标记返回或回显您选择的分类中的类别或术语的下拉菜单 (