Template Tags: Tuts+ Tutorial Continued

PHPz
Release: 2023-08-31 18:18:01
Original
1015 people have browsed it

模板标签:Tuts+ 指南继续

In part two of this series, we introduced the first WordPress template tags. In Part 3, we'll introduce the second batch of nearly 200 template tags. Throughout the tutorial, we'll see template tags for taxonomies and terms.

Get the description of the category: category_description()

This template tag returns the description of the category.

parameter

This template tag accepts only one parameter:

    <li> $category_ID (optional - integer) :

    Category ID.

    (default: query category) >

usage

<?php

$cat_desc = get_category_description( 15 );

?>
Copy after login

Get the description of the tag: tag_description()

This template tag returns a description of the tag.

parameter

This template tag accepts only one parameter:

    <li> $tag_ID (optional - integer) :

    The ID of the tag.

    (Default: current label)

usage

<?php

// Display the description of current tag.
echo tag_description();

// Get the description of a specific tag.
$special_tag_desc = tag_description( 42 );

?>
Copy after login

Get the description of the term: term_description()

This template tag only returns the description of the term.

parameter

This template tag accepts two parameters:

    <li> $term_ID (optional - integer) :

    The ID of the term.

    (default: current term) <li> $taxonomy (optional - string) :

    The classification of the term.

    (Default: 'post_tag') >

usage

<?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' );

?>
Copy after login

Get and display the page title of the category file: single_cat_title()

This template tag returns and displays the title of the category for use in page titles.

parameter

This template tag accepts two parameters:

    <li> $prefix (optional - string) :

    Title prefix.

    (default: empty) <li> $echo (optional - boolean) :

    Whether to echo (TRUE) or return (FALSE) Label.

    (Default: TRUE)

usage

<?php

// Display the category title with a prefix.
single_cat_title( 'Category: ' );

// Get the category title.
$cat_title = single_cat_title( '', false );

?>
Copy after login

Get and display the page title of the tag file: single_tag_title()

This template tag returns and displays the tag's title for use in page titles.

parameter

This template tag accepts two parameters:

    <li> $prefix (optional - string) :

    Title prefix.

    (default: empty) <li> $echo (optional - boolean) :

    Whether to echo (TRUE) or return (FALSE) Label.

    (Default: TRUE)

usage

<?php

// Display the tag title with a prefix.
single_tag_title( 'Tag Archive: ' );

// Get the tag title.
$tag_title = single_tag_title( '', false );

?>
Copy after login

Get and display the page title of the term archive: single_term_title()

This template tag returns and displays the term title used in the page title.

parameter

This template tag accepts two parameters:

    <li> $prefix (optional - string) :

    Title prefix.

    (default: empty) <li> $echo (optional - boolean) :

    Whether to echo (TRUE) or return (FALSE) Label.

    (Default: TRUE)

usage

<?php

// Display the term title with a prefix.
single_term_title( 'Term Archive: ' );

// Get the term title.
$term_title = single_term_title( '', false );

?>
Copy after login

Get and display the category of the post: get_the_category_list() & the_category()

These template tags get and output the category assigned to the post.

parameter

Both template tags accept three parameters:

    <li> $separator (optional - string) :

    Separator text.

    (Default: empty - if this parameter is empty, the function returns and displays an unordered list of categories) <li> $parents (optional - string) :

    How to display parent categories ("multiple", "single", or empty).

    (default: empty) <li> $post_ID (optional - integer) :

    Post ID.

    (Default: current post)

usage

<?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' );

?>
Copy after login

Get and display the categories of feed posts: get_the_category_rss() & the_category_rss()

These template tags prepare the category assigned to the post for feeding and returning or displaying it.

parameter

Two template tags only accept one parameter:

    <li> $feed_type (optional - string) :

    Feed type.

    (default: default feed type)

usage

<?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' );

?>
Copy after login

Get and display the tags of the post: get_the_tag_list() & the_tags()

These template tags return or display tags for your posts.

parameter

Both template tags accept three parameters:

    <li> $before (optional — string) :

    Text or HTML code to display before output.

    (默认值:为空get_the_tag_list() 和 '标签:' the_tags()) <li> $sep (可选 — 字符串)

    用作分隔符的文本。

    (默认值: get_the_tag_list() 和 ', ' 表示 the_tags()) <li> $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>' );

?>
Copy after login

获取并显示帖子的条款:get_the_term_list() & the_terms()

这些模板标签获取并回显为帖子分配的术语列表。

参数

两个模板标签都接受五个参数:

    <li> $post_ID (可选 - 整数)

    帖子 ID。

    (默认:当前帖子) <li> $taxonomy (必需 - 字符串)

    显示其术语的分类法。

    (默认:空) <li> $before (可选—字符串)

    在输出之前显示的文本或 HTML 代码。

    (默认:空) <li> $sep (可选 — 字符串)

    分隔符文本。

    (默认:', ') <li> $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' ), ' - ' );

?>
Copy after login

显示帖子的分类法:the_taxonomies()

此模板标记显示分类法以及与这些分类法关联的术语。

参数

此模板标记仅接受一个参数:

    <li> $args (可选—数组)

    以下参数的数组:

      <li> 'post'(整数):帖子 ID。

      (默认:0) <li> 'before'(字符串):在输出之前添加的 HTML 代码。

      (默认:空) <li> 'sep'(字符串):分隔符文本。

      (默认:' ') <li> 'after'(字符串):在输出后添加的 HTML 代码。

      (默认:空) <li> 'template'(字符串):用于显示分类术语的模板。

      (默认值:'%s: %l。',其中 %s 是分类标签%t 是术语链接列表)

用法

<?php

$args = array(
    'before' => '<div class="taxonomies">',
	'sep'    => ', ',
	'after'  => '</div>'
);

the_taxonomies( $args );

?>
Copy after login

获取并显示“编辑标签”链接:get_edit_tag_link() & edit_tag_link()

这些模板标记返回或回显“编辑此标记”链接,供您在模板中使用。

参数

get_edit_tag_link() 接受两个参数:

    <li> $tag_ID (必需 - 整数)

    标签的 ID。

    (默认:NULL > <li> $taxonomy (可选—字符串)

    分类法的类型。

    (默认:'post_tag')

edit_tag_link() 接受四个参数:

    <li> $link (可选—字符串)

    链接显示的文本。

    (默认:“编辑此”)< /em> <li> $before (可选—字符串)

    在输出之前显示的文本或 HTML 代码。

    (默认:空) <li> $after (可选—字符串)

    输出后显示的文本或 HTML 代码。

    (默认:空) <li> $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 );

?>
Copy after login

获取并显示类别列表:wp_list_categories()

此模板标记返回或显示任何类型分类法中的类别或术语列表。

参数

此模板标记仅接受一个参数:

    <li> $args (可选—数组)

    以下参数的数组:

      <li> 'show_option_all'(字符串):如果将其设置为非空值,则显示主页(或设置>阅读中设置的“帖子”页面)的链接。该值将是链接的锚文本。

      (默认:空) <li> 'show_option_none'(字符串):如果没有任何类别,则显示文本。

      (默认:“无类别”) <li> 'orderby'(字符串):如何对链接进行排序。接受“name”、“ID”、“slug”、“count”和“term_group”。

      (默认:“name”) <li> 'order'(字符串):是否按升序('ASC')或降序('DESC')排序项目。

      (默认:'ASC') <li> 'style'(字符串):显示类别的样式 - 'list' 生成无序列表或 'none' 生成以 <br />s 分隔的链接列表。

      (默认:'列表') <li> 'show_count'(整数):在每个类别链接旁边显示帖子计数。

      (默认值:0) <li> 'hide_empty'(整数):是否隐藏空类别。

      (默认:1) <li> 'use_desc_for_title'(整数):使用描述作为链接的“标题”属性。

      (默认:1) <li> 'child_of'(整数):仅显示类别给定 ID 的子级。

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

      (默认:空) <li> 'feed_type'(字符串):提要类型('rss'、'rss2'、'atom' 或 'rdf')。

      (默认:空) <li> 'feed_image'(字符串):覆盖锚文本并显示图像的图像路径。

      (默认:空) <li> 'exclude'(字符串):要排除的以逗号分隔的类别 ID 列表。

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

      (默认:空) <li> 'current_category'(整数):在合适的情况下添加“current-cat”类。

      (默认:0) <li> 'hierarchical'(整数):是否显示具有分层顺序的项目。

      (默认:1) <li> 'title_li'(字符串):使用 <li> 标签包装输出,并在类别列表之前显示标题。您可能希望将其设置为空字符串并禁用换行。

      (默认:“类别”) <li> 'echo'(整数):是否回显输出(1)或返回它(0)。

      (默认:1) <li> 'depth'(整数):最大深度。

      (默认:0) <li> '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 );

?>
Copy after login

获取并显示 HTML 类别下拉列表:wp_dropdown_categories()

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

参数

此模板标记仅接受一个参数:

    <li> $args (可选—数组)

    以下参数的数组:

      <li> 'show_option_all'(字符串):用于显示所有类别/分类的文本。

      (默认:空) <li> 'show_option_none'(字符串):显示不显示类别/分类的文本。

      (默认:空) <li> 'option_none_value'(混合):未选择类别/分类时使用的值。

      (默认值:-1) <li> 'orderby'(字符串):用于对类别/分类进行排序的列。

      (默认:'ID') <li> 'order'(字符串):按什么方向排序类别/分类。

      (默认:'ASC') <li> 'show_count'(布尔值或整数):是否显示类别/分类中有多少帖子。

      (默认:0) <li> 'hide_empty'(布尔值或整数):是否隐藏没有附加任何帖子的类别/分类法。

      (默认:1) <li> 'child_of'(整数):显示给定 ID 的后代的所有类别。

      (默认值:0) <li> 'exclude'(字符串):要从列表中排除的以逗号分隔的类别/分类 ID 列表。

      (默认:空) <li> 'echo'(布尔值或整数):是否显示或检索内容。

      (默认:1) <li> 'depth'(整数):最大深度。

      (默认:0) <li> 'tab_index'(整数):SELECT 元素的选项卡索引。

      (默认值:0) <li> 'name'(字符串):SELECT 元素的 NAME 属性值。

      (默认:'cat') <li> 'id'(字符串):SELECT 元素的 ID。

      (默认:空) <li> 'class'(字符串):SELECT 元素的 CSS 类。

      (默认:'postform') <li> 'selected'(整数):默认选择的类别/分类ID。

      (默认:0) <li> 'taxonomy'(字符串):要检索的分类法的名称。

      (默认:'category')

用法

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

?>
Copy after login

获取并显示标签云:wp_tag_cloud()

此模板标签返回或显示根据您选择的分类法(默认为标签)生成的术语云。

参数

此模板标记仅接受一个参数:

    <li> $args (可选—数组)

    以下参数的数组:

      <li> 'smallest'(整数):最小文本大小。

      (默认:8) <li> 'largest'(整数):最大文本大小。

      (默认:22) <li> 'unit'(字符串):文本大小单位。

      (默认:'pt') <li> 'number'(字符串):返回多少个标签

      (默认:45) <li> 'format'(字符串):如何返回标签列表 - 'flat' 表示以空格分隔的平面列表,'list' 表示 HTML 无序列表,'array' 表示 PHP 数组。

      (默认:'平坦') <li> 'separator'(字符串):分隔符文本。

      (默认:“\n”) <li> 'orderby'(字符串):如何按('name' 或 'count')对标签进行排序。

      (默认:'name') <li> 'order'(字符串):是否按升序('ASC')或降序('DESC')排序项目。

      (默认:'ASC') <li> 'include'(数组):要包含的 ID 数组。

      (默认:空) <li> 'exclude'(数组):要排除的 ID 数组。

      (默认:空) <li> 'taxonomy'(字符串):从中获取术语的分类法。

      (默认:'post_tag') <li> 'link' (string): 'view' 为前端查看, 'edit' 为后端编辑链接。与“post_type”参数一起使用。

      (默认:“view”) <li> 'post_type'(字符串):如果'link'参数设置为'edit',则必须将'post_type'设置为帖子类型,以设置“编辑链接”列表中的帖子类型。

      (默认:空) <li> 'echo' (boolean): 是否回显模板标签。

      (默认: 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 );

?>
Copy after login

获取自定义生成的标签云:wp_generate_tag_cloud()

此模板标签返回标签云,但这一次,您可以选择自己的标签。

参数

此模板标记接受两个参数:

    <li> $tags (必需 — 数组)

    标签列表的数组。

    (默认:NULL)< /em> <li> $args (可选—数组)

    以下参数的数组:

      <li> 'smallest'(整数):最小文本大小。

      (默认:8) <li> 'largest'(整数):最大文本大小。

      (默认:22) <li> 'unit'(字符串):文本大小单位。

      (默认:'pt') <li> 'number'(字符串):返回多少个标签

      (默认:0) <li> 'format'(字符串):如何返回标签列表 - 'flat' 表示以空格分隔的平面列表,'list' 表示 HTML 无序列表,'array' 表示 PHP 数组。

      (默认:'平坦') <li> 'separator'(字符串):分隔符文本。

      (默认:“\n”) <li> 'orderby'(字符串):如何按('name' 或 'count')对标签进行排序。

      (默认:'name') <li> 'order'(字符串):是否按升序('ASC')或降序('DESC')排序项目。

      (默认:'ASC') <li> 'topic_count_text'(字符串):这是 _n_noop() 的 noooped 复数形式,用于生成标签链接工具提示的文本。

      (默认:NULL <li> 'topic_count_text_callback'(字符串):这是回调函数,它给出带有该标签的帖子的计数,返回标签链接工具提示的文本。

      (默认: NULL) <li> 'topic_count_scale_callback'(字符串):这是与 'smallest''largest' 参数一起使用的回调函数,用于计算生成标签的字体大小。

      < em>(默认:'default_topic_count_scale') <li> '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 );

?>
Copy after login

结论

希望您喜欢第二批模板标签。还有六批要进行,请继续关注更多模板标签!

如果您有任何问题、意见或更正,您可以在评论部分与我们分享您的想法。如果您喜欢这篇文章,请不要忘记与您的朋友分享!

The above is the detailed content of Template Tags: Tuts+ Tutorial Continued. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!