게시물 읽기 기간 WordPress 단축 코드
며칠 전에 페이지 상단에 페이지를 읽는 데 걸리는 예상 시간이라는 약간의 추가 정보를 제공하는 웹사이트를 봤습니다. 나는 그것이 재미있다고 생각했고 WordPress 단축 코드를 사용하여 비슷한 것을 만들고 싶었습니다.
이 작업은 매우 쉬우므로 시작해 보겠습니다.
1단계: 플러그인 생성
평소처럼 플러그인을 만들 예정이므로 wp-content/plugins 아래에 새 폴더를 만들고 이름을 "post-reading-duration-shortcode"로 지정합니다. 그것을 열고 "post-reading-duration-shortcode.php"라는 파일을 만듭니다. 파일을 열고 다음 코드를 붙여넣습니다.
<?php
/*
Plugin Name: Post Reading Duration Shortcode
Plugin URL: http://remicorson.com/
Description: Display the estimated time to read the post
Version: 0.1
Author: Remi Corson
Author URI: http://remicorson.com
Contributors: corsonr
Text Domain: rc_prds
Domain Path: languages
*/파일을 저장하면 플러그인 페이지에 플러그인 목록이 표시됩니다.
2단계: 단축 코드 생성
기본적으로 플러그인은 속성 없이 간단한 단축 코드를 제공합니다. 텍스트는 단축 코드 태그 사이에 캡슐화됩니다. 단축 코드는 방문자가 게시물을 읽어야 하는 시간을 프런트엔드에 표시합니다. 단축 코드를 만들려면 다음 코드를 사용하세요.
/**
* Register the shortcode
*
* @access public
* @since 1.0
* @return void
*/
function rc_prds_add_reading_duration_shortcode( $atts, $content = null ) {
return '<span class="reading_duration">'.$content.'</span>';
}
add_shortcode("reading_duration", "rc_prds_add_reading_duration_shortcode");파일을 저장할 수 있습니다. 게시물이나 페이지에 단축 코드 [reading_duration]예상 독서 시간: XX[/reading_duration]을 추가하면 '예상 독서 시간: XX'가 'span' 태그로 캡슐화되어 표시되어야 합니다.
다음 단계는 미리 정의된 분당 읽은 단어 수에 따라 페이지나 게시물을 읽는 데 필요한 실제 예상 시간을 표시하도록 단축 코드를 수정하는 것입니다.
그러기 위해서는 숏코드 코드를 진화시켜야 합니다. (str_word_count()를 사용하여 게시물의 단어 수를 검색하려면) 게시물 ID를 가져와야 하며, 사람들이 일반적으로 1분 동안 읽는 단어 수를 정의해야 합니다. 분당 200단어라고 해보자.
/**
* Register the shortcode
*
* @access public
* @since 1.0
* @return void
*/
function rc_prds_add_reading_duration_shortcode( $atts, $content = null ) {
global $post;
// Get Post ID
$post_id = $post->ID;
// Words read per minute (you can set your own value)
$words_per_minute = 200;
// Get estimated time
$estimated_reading_time = rc_prds_get_post_reading_time( $post_id, $words_per_minute );
return '<span class="reading_duration">'.$content.' '.$estimated_reading_time.'</span>';
}
add_shortcode("reading_duration", "rc_prds_add_reading_duration_shortcode");3단계: 카운터 기능
보시다시피 정의되지 않은 함수인 rc_prds_get_post_reading_time()이 있습니다. 게시물을 읽는 데 필요한 예상 시간을 반환하는 함수입니다. 만들어 봅시다:
/**
* Get post reding time
*
* @access public
* @since 1.0
* @return void
*/
function rc_prds_get_post_reading_time( $post_id, $words_per_minute ){
// Get post's words
$content = get_the_content( $post_id );
$count_words = str_word_count( strip_tags( $content ) );
// Get Estimated time
$minutes = floor( $count_words / $words_per_minute);
$seconds = floor( ($count_words / ($words_per_minute / 60) ) - ( $minutes * 60 ) );
// If less than a minute
if( $minutes < 1 ) {
$estimated_time = __( 'Less than a minute', 'rc_prds' );
}
// If more than a minute
if( $minutes >= 1 ) {
if( $seconds > 0 ) {
$estimated_time = $minutes.__( 'min', 'rc_prds' ).' '.$seconds.__( 'sec', 'rc_prds' );
} else {
$estimated_time = $minutes.__( 'min', 'rc_prds' );
}
}
return $estimated_time;
}기본적으로 이 함수는 "get_the_content()"에 있는 단어 수를 계산한 다음 게시물을 읽는 데 필요한 분과 초를 계산합니다. 마지막 부분은 $mins 및 $seconds 값에 따라 올바른 메시지를 표시하는 간단한 방법입니다.
더 나아가서 게시물을 읽는 데 필요한 시간을 추가할 수 있지만 이것이 필수인지 100% 확신할 수 없습니다!
이제 다음을 추가하기만 하면 단축 코드를 완전히 사용할 수 있습니다.
[reading_duration]Estimated Reading Time:[/reading_duration]