get_comments_number_text()WP 4.0.0

Получает локализованную надпись о количестве комментариев записи (поста). Уведомления и пинги включаются в подсчет. Для использования внутри Цикла WordPress.

Работает на основе: get_comments_number(), number_format_i18n()
Основа для: comments_number()
Хуки из функции

Возвращает

Строку. Надпись например 15 Комментариев.

Использование

get_comments_number_text( $zero, $one, $more, $post_id );
$zero(строка)
Текст, который будет показан, если комментариев нет.
По умолчанию: 'Комментариев нет'
$one(строка)
Текст, который будет показан, если всего 1 комментарий.
По умолчанию: '1 комментарий'
$more(строка)
Текст, который будет показан, если комментариев больше чем 1.
По умолчанию: '% комментариев'
$post_id(число/WP_Post)
ID или объект поста, кол-во комментов которого нужно получить.
По умолчанию: 0 (global $post)

Примеры

-1

#1 Количество комментариев статьи

Выведем количество комментариев текущей записи и укажем какой текст писать, при определенном количестве комментариев.

<p>
У этого поста <?php echo get_comments_number_text( 'пока нет комментариев', '1 комментарий', '% комментариев'); ?>.
</p>

Список изменений

С версии 4.0.0 Введена.
С версии 5.4.0 Added the $post parameter to allow using the function outside of the loop.

Код get_comments_number_text() WP 6.5.2

function get_comments_number_text( $zero = false, $one = false, $more = false, $post = 0 ) {
	$comments_number = get_comments_number( $post );

	if ( $comments_number > 1 ) {
		if ( false === $more ) {
			$comments_number_text = sprintf(
				/* translators: %s: Number of comments. */
				_n( '%s Comment', '%s Comments', $comments_number ),
				number_format_i18n( $comments_number )
			);
		} else {
			// % Comments
			/*
			 * translators: If comment number in your language requires declension,
			 * translate this to 'on'. Do not translate into your own language.
			 */
			if ( 'on' === _x( 'off', 'Comment number declension: on or off' ) ) {
				$text = preg_replace( '#<span class="screen-reader-text">.+?</span>#', '', $more );
				$text = preg_replace( '/&.+?;/', '', $text ); // Remove HTML entities.
				$text = trim( strip_tags( $text ), '% ' );

				// Replace '% Comments' with a proper plural form.
				if ( $text && ! preg_match( '/[0-9]+/', $text ) && str_contains( $more, '%' ) ) {
					/* translators: %s: Number of comments. */
					$new_text = _n( '%s Comment', '%s Comments', $comments_number );
					$new_text = trim( sprintf( $new_text, '' ) );

					$more = str_replace( $text, $new_text, $more );
					if ( ! str_contains( $more, '%' ) ) {
						$more = '% ' . $more;
					}
				}
			}

			$comments_number_text = str_replace( '%', number_format_i18n( $comments_number ), $more );
		}
	} elseif ( 0 == $comments_number ) {
		$comments_number_text = ( false === $zero ) ? __( 'No Comments' ) : $zero;
	} else { // Must be one.
		$comments_number_text = ( false === $one ) ? __( '1 Comment' ) : $one;
	}

	/**
	 * Filters the comments count for display.
	 *
	 * @since 1.5.0
	 *
	 * @see _n()
	 *
	 * @param string $comments_number_text A translatable string formatted based on whether the count
	 *                                     is equal to 0, 1, or 1+.
	 * @param int    $comments_number      The number of post comments.
	 */
	return apply_filters( 'comments_number', $comments_number_text, $comments_number );
}