函数文档

print_embed_comments_button()

💡 云策文档标注

概述

print_embed_comments_button() 函数用于输出嵌入评论按钮的 HTML 标记,仅在非 404 页面且评论可用时生效。

关键要点

  • 函数检查 is_404()、get_comments_number() 和 comments_open() 条件,避免在不适用场景输出
  • 使用 _n() 函数根据评论数量输出单数或复数形式的文本
  • 输出包含评论链接的按钮标记,适用于嵌入内容

代码示例

function print_embed_comments_button() {
    if ( is_404() || ! ( get_comments_number() || comments_open() ) ) {
        return;
    }
    ?>
    <div class="wp-embed-comments">
        <a href="<?php comments_link(); ?>" target="_top">
            <?php
            printf(
                _n(
                    '%s Comment',
                    '%s Comments',
                    get_comments_number()
                ),
                number_format_i18n( get_comments_number() )
            );
            ?>
        </a>
    </div>
    <?php
}

注意事项

  • 函数在 WordPress 4.4.0 版本引入,需确保版本兼容性
  • 依赖多个 WordPress 核心函数,如 comments_link() 和 number_format_i18n(),调用前应确认环境支持

📄 原文内容

Prints the necessary markup for the embed comments button.

Source

function print_embed_comments_button() {
if ( is_404() || ! ( get_comments_number() || comments_open() ) ) {
return;
}
?>
<div class="wp-embed-comments">
<a href="<?php comments_link(); ?>" target="_top">
<span class="dashicons dashicons-admin-comments"></span>
Comment</span>',
'%s <span class="screen-reader-text">Comments</span>',
get_comments_number()
),
number_format_i18n( get_comments_number() )
);
?>
</a>
</div>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/embed.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/embed.php#L1134">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/embed.php#L1134-L1156">View on GitHub</a></p></section>

<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/_n/">_n()</a><code>wp-includes/l10n.php

Translates and retrieves the singular or plural form based on the supplied number.

is_404()wp-includes/query.php

Determines whether the query has resulted in a 404 (returns no results).

comments_open()wp-includes/comment-template.php

Determines whether the current post is open for comments.

get_comments_number()wp-includes/comment-template.php

Retrieves the amount of comments a post has.

comments_link()wp-includes/comment-template.php

Displays the link to the current post comments.

number_format_i18n()wp-includes/functions.php

Converts float number to format based on the locale.

Show 1 moreShow less

Changelog

Version Description
4.4.0 Introduced.