Created
August 7, 2021 14:23
-
-
Save artikus11/b9b8a512fa9219a4fd6d49ef1f2f667e to your computer and use it in GitHub Desktop.
Функция подсчета времени чтения поста
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Возвращает примерное время чтение поста, в зависимости от количества знаков | |
* | |
* @param int $post_id ID поств, для которого надо расчитать время | |
* | |
* @return string | |
* | |
* @author Artem Abramovich | |
* @verphp 7.0 | |
*/ | |
function get_time_for_read( $post_id = 0 ): string { | |
if ( isset( $post_id->ID ) ) { | |
$post_id = $post_id->ID; | |
} | |
if ( ! $post_id ) { | |
$post_id = get_the_ID(); | |
} | |
$post = get_post( $post_id ); | |
$post_content_length = strlen( sanitize_textarea_field( $post->post_content ) ); | |
$time_for_read = round( $post_content_length / 1500 ); | |
return sprintf( | |
'%s %s', | |
$time_for_read, | |
get_num_ending( $time_for_read, [ 'минута', 'минуты', 'минут' ] ) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment