Comments: Add a `$post_id` parameter to `get_comments_number_text()` and `comments_number()`.

This allow for using these functions outside of the loop.

Props donmhico, garrett-eclipse, Hinjiriyo, imath.
Fixes #48772.
Built from https://develop.svn.wordpress.org/trunk@47276


git-svn-id: http://core.svn.wordpress.org/trunk@47076 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2020-02-11 20:41:05 +00:00
parent be2ec734f2
commit 17e563cd9c
2 changed files with 17 additions and 12 deletions

View File

@ -870,30 +870,35 @@ function get_comments_number( $post_id = 0 ) {
* Displays the language string for the number of comments the current post has. * Displays the language string for the number of comments the current post has.
* *
* @since 0.71 * @since 0.71
* @since 5.4.0 Added the `$post_id` parameter.
* *
* @param string $zero Optional. Text for no comments. Default false. * @param string $zero Optional. Text for no comments. Default false.
* @param string $one Optional. Text for one comment. Default false. * @param string $one Optional. Text for one comment. Default false.
* @param string $more Optional. Text for more than one comment. Default false. * @param string $more Optional. Text for more than one comment. Default false.
* @param string $deprecated Not used. * @param string $deprecated Not used.
* @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`.
*/ */
function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) { function comments_number( $zero = false, $one = false, $more = false, $deprecated = '', $post_id = 0 ) {
if ( ! empty( $deprecated ) ) { if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '1.3.0' ); _deprecated_argument( __FUNCTION__, '1.3.0' );
} }
echo get_comments_number_text( $zero, $one, $more ); echo get_comments_number_text( $zero, $one, $more, $post_id );
} }
/** /**
* Displays the language string for the number of comments the current post has. * Displays the language string for the number of comments the current post has.
* *
* @since 4.0.0 * @since 4.0.0
* @since 5.4.0 Added the `$post_id` parameter to allow using the function outside of the loop.
* *
* @param string $zero Optional. Text for no comments. Default false. * @param string $zero Optional. Text for no comments. Default false.
* @param string $one Optional. Text for one comment. Default false. * @param string $one Optional. Text for one comment. Default false.
* @param string $more Optional. Text for more than one comment. Default false. * @param string $more Optional. Text for more than one comment. Default false.
* @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`.
* @return string Language string for the number of comments a post has.
*/ */
function get_comments_number_text( $zero = false, $one = false, $more = false ) { function get_comments_number_text( $zero = false, $one = false, $more = false, $post_id = 0 ) {
$number = get_comments_number(); $number = get_comments_number( $post_id );
if ( $number > 1 ) { if ( $number > 1 ) {
if ( false === $more ) { if ( false === $more ) {

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.4-alpha-47275'; $wp_version = '5.4-alpha-47276';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.