get_comment_excerpt() WP 1.5.0
Получает обрезок текущего или указанного комментария (максимум 20 слов).
Если текст был обрезан, то в конце добавиться …
.
На выходе не обрабатывается функцией wpautop().
1 раз — 0.000609 сек (медленно) | 50000 раз — 5 сек (быстро)
Возвращает
Строку. Обрезанный/необрезанный текст.
Использование
get_comment_excerpt( $comment_ID );
- $comment_ID(число/WP_Comment) (обязательный)
- ID или объект комментария, который нужно проверить на длину, и если надо, то обрезать.
По умолчанию: 0 (текущий комментарий в цикле)
Примеры
#1 Обрежем текст комментария до 20 слов, если он больше.
echo get_comment_excerpt( 1024 );
// выведет:
// Посмотрел, краем глаза Get Recent Comments - наворотили гору кода :) Протестировал свой код запросы создает стандартная функция WordPress…
Список изменений
С версии 1.5.0 |
Введена. |
С версии 4.4.0 |
Added the ability for $comment_ID to also accept a WP_Comment object. |
Код get_comment_excerpt() get comment excerpt
WP 5.6.2
<?php
function get_comment_excerpt( $comment_ID = 0 ) {
$comment = get_comment( $comment_ID );
if ( ! post_password_required( $comment->comment_post_ID ) ) {
$comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
} else {
$comment_text = __( 'Password protected' );
}
/* translators: Maximum number of words used in a comment excerpt. */
$comment_excerpt_length = (int) _x( '20', 'comment_excerpt_length' );
/**
* Filters the maximum number of words used in the comment excerpt.
*
* @since 4.4.0
*
* @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
*/
$comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );
$excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' );
/**
* Filters the retrieved comment excerpt.
*
* @since 1.5.0
* @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
*
* @param string $excerpt The comment excerpt text.
* @param int $comment_ID The comment ID.
* @param WP_Comment $comment The comment object.
*/
return apply_filters( 'get_comment_excerpt', $excerpt, $comment->comment_ID, $comment );
}
Cвязанные функции