Tuts+ Guide to the Third Batch of Template Tags

WBOY
Release: 2023-08-31 20:42:01
Original
786 people have browsed it

Tuts+ Guide to the Third Batch of Template Tags

In part three of this series, we covered the second batch of WordPress template tags. In Part 4, we introduce the third batch of nearly 200 template tags. Throughout this tutorial we'll see template tags for comments.

Get and display the comment author name: get_comment_author() & comment_author()

These template tags return or display the commenter's name.

parameter

Two template tags only accept one parameter:

  • $comment_ID (optional - integer) :

    The ID of the comment to use.

    (Default: ID of current comment)

usage

<?php

// Display the commenter's name.
comment_author();

// Build an introduction of each comment.
$comment_intro = get_comment_author() . __( ' says...', 'translation-domain' );

?>
Copy after login

Get and display the feed's comment author name: get_comment_author_rss() & comment_author_rss()

These template tags return or echo the name of the comment author and prepare it for display on the feed.

parameter

These template tags do not accept any parameters.

usage

<?php

comment_author_rss();

?>
Copy after login

Get and display the email address of the comment author: get_comment_author_email() & comment_author_email()

These template tags allow you to return or echo the commenter's email address. (Warning: It's not cool to display the commenter's email address to the public on the front end, so make sure you use it correctly.)

parameter

Two template tags only accept one parameter:

  • $comment_ID (optional - integer) :

    The ID of the comment to use.

    (Default: ID of current comment)

usage

<?php

// Get the email address of the commenter.
comment_author_email();

// Return the email address of the commenter from a specific comment.
$commenter_email = get_comment_author_email( 57 );

?>
Copy after login

Get and display the link to the comment author's email address: get_comment_author_email_link() & comment_author_email_link()

These template tags allow you to return or echo the commenter's email address in the form of a mailto: link.

parameter

Both template tags accept three parameters:

  • $linktext (optional - string) :

    Text to display instead of the email address of the comment author.

    (default: email address)

  • $before (optional - string) :

    Text or HTML code to display before output.

    (default: empty)

  • $after (optional - string) :

    Text or HTML code to be displayed after output.

    (default: empty)

usage

<?php

// Get the email link of the commenter.
comment_author_email_link();

// Return the email link of the commenter.
$commenter_email_link = get_comment_author_email_link( '<i class="icon-email"></i>', __( 'Comment Author\'s Email Address', 'translation-domain' ), '<br />' );

?>
Copy after login

Get and display the commenter's URL: get_comment_author_url() & comment_author_url()

These template tags allow you to return or display the URL of the review author's website.

parameter

Two template tags only accept one parameter:

  • $comment_ID (optional - integer) :

    The ID of the comment to use.

    (Default: ID of current comment)

usage

<?php

// Display the comment author url.
comment_author_url();

// Return the comment author url.
$commenter_URL = get_comment_author_url();

// Return a link to the comment author's website from a specific comment.
$commenter_link = '<a href="' . get_comment_author_url( 988 ) . '">' . __( 'Comment Author\'s Website', 'translation-domain' ) . '</a>';

?>
Copy after login

Get and display the commenter's link (with the author's name as anchor text): get_comment_author_link() & comment_author_link()

These template tags get or echo the commenter's website link and use the commenter's name as anchor text.

parameter

Two template tags only accept one parameter:

  • $comment_ID (optional - integer) :

    The ID of the comment to use.

    (Default: ID of current comment)

usage

<?php

// Display the comment author's link.
comment_author_link();

// Return the comment author's link from a specific comment.
$commenter_link = get_comment_author_link( 452 );

?>
Copy after login

Get and display the commenter's link (using custom text): get_comment_author_url_link() & comment_author_url_link()

These template tags allow you to get or echo a link to the commenter's website, anchored with custom text.

parameter

Both template tags accept three parameters:

  • $linktext (optional - string) :

    The text to be displayed.

    (Default: URL)

  • $before (optional - string) :

    Text or HTML code to display before output.

    (default: empty)

  • $after (optional - string) :

    Text or HTML code to be displayed after output.

    (default: empty)

usage

<?php

// Display a customized "commenter's website" link.
comment_author_url_link( __( 'Comment author\'s website', 'translation-domain' ) );

// Return a customized "commenter's website" link with $before and $after.
$comment_author_website = get_comment_author_url_link( __( 'Comment author\'s website', 'translation-domain' ), '<span class="icon-website">', '</span>' );

?>
Copy after login

Get and display the commenter’s IP address: get_comment_author_IP() & comment_author_IP()

These template tags return or display the IP address of the comment author.

parameter

Two template tags only accept one parameter:

  • $comment_ID (optional - integer) :

    The ID of the comment to use.

    (默认:当前评论的 ID)

用法

<?php

// Display the comment author's IP.
comment_author_IP();

// Display the comment author's IP from a specific comment.
$commenter_IP = get_comment_author_IP( 41 );

?>
Copy after login

获取并显示评论内容: get_comment_text() & comment_text()

这些模板标签获取并显示评论的内容。

参数

两个模板标签只接受一个参数:

  • $comment_ID (可选 - 整数)

    要使用的评论的 ID。

    (默认:当前评论的 ID)

用法

<?php

// Display the current comment's content.
comment_text();

// Get a specific comment's content.
$comment_content = get_comment_text( 965 );

?>
Copy after login

显示 Feed 的评论内容:comment_text_rss()

此模板标记获取评论内容并使其准备好在提要中显示。

参数

此模板标记不接受任何参数。

用法

<?php

comment_text_rss();

?>
Copy after login

获取并显示评论摘录: get_comment_excerpt() & comment_excerpt()

这些模板标签获取评论的内容并将其剪切以显示其“摘录”。

参数

两个模板标签只接受一个参数:

  • $comment_ID (可选 - 整数)

    要使用的评论的 ID。

    (默认:当前评论的 ID)

用法

<?php

// Echo the current comment's excerpt.
comment_excerpt();

// Return a given comment's excerpt.
$comment_excerpt = get_comment_excerpt( 355 );

?>
Copy after login

获取并显示评论日期:get_comment_date() & comment_date()

这些模板标记回显或返回发布评论的日期。

参数

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

  • $date_format (可选—字符串)

    日期的格式。

    (默认:常规选项中设置的日期格式页)

  • $comment_ID (可选 - 整数)

    要使用的评论的 ID。

    (默认:当前评论的 ID)

用法

<?php

// Display the current comment's date.
comment_date();

// Get a specific comment's date with a special date format.
$some_comment_date = get_comment_date( 'MM/DD/YYYY', 9812 );

?>
Copy after login

获取并显示评论时间:get_comment_time() & comment_time()

这些模板标签返回或回显评论发布的时间。

参数

get_comment_time() 接受三个参数:

  • $time_format (可选—字符串)

    时间的格式。

    (默认:常规选项中设置的时间格式页)

  • $gmt (可选 - 布尔值)

    是否使用 GMT 日期。

    (默认:FALSE

  • $translate (可选—布尔值)

    是否传递给 date_i18n() 函数来翻译日期。

    (默认:TRUE

comment_time() 只接受一个参数:

  • $time_format (可选—字符串)

    时间的格式。

    (默认:常规选项中设置的时间格式页)

用法

<?php

// Display the current comment's time.
comment_time();

// Get a specific comment's time with a special time format.
$some_comment_time = get_comment_time( 'H:i:s', 115 );

?>
Copy after login

获取并显示评论 ID:get_comment_ID() & comment_ID()

这些模板标签的工作非常简单:它们获取评论的 ID。

参数

这些模板标记不接受任何参数。

用法

<?php

comment_ID();

?>
Copy after login

显示评论的类型comment_type()

此模板标记可让您显示评论的类型 - 普通评论、引用通告或 pingback。

参数

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

  • $commenttxt (可选—字符串)

    “评论”类型显示的文本。

    (默认:“评论”)

  • $trackbacktxt (可选—字符串)

    为“引用引用”类型显示的文本。

    (默认:“引用引用”)

  • $pingbacktxt (可选 — 字符串)

    显示“pingback”类型的文本。

    (默认:“Pingback”)

用法

<?php

// Display comment type with default texts.
comment_type();

// Display comment type with custom texts.
comment_type( __( 'Reaction', 'translation-domain' ), __( 'Trackback', 'translation-domain' ), __( 'Ping', 'translation-domain' ) );

?>
Copy after login

获取用户头像:get_avatar()

此模板标签可让您获取用户的“头像”。

参数

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

  • $id_or_email (必需 — 字符串、整数或对象)

    用户 ID、电子邮件地址或评论对象。

    (默认值:NULL)

  • $size (可选 - 整数)

    头像的大小(以像素为单位)。

    (默认:96) >

  • $default (可选 - 字符串)

    自定义“默认图像”的 URL(如果没有可用的头像)。

    (默认值:空)

  • $alt (可选—字符串)

    图像的替代文本(alt 参数)。

    (默认: FALSE)

用法

<?php

// Returns current comment author's avatar.
$commenter_email = get_comment_author_email();
$commenter_avatar = get_avatar( $commenter_email, 512 );

?>
Copy after login

结论

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

If you have any questions, comments, or corrections, you can share your thoughts with us in the comments section. If you liked this article, don’t forget to share it with your friends!

The above is the detailed content of Tuts+ Guide to the Third Batch of Template Tags. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!