PHP - 워드프레스 게시물 조회수 집계 쇼트코드 생성하기
1단계:
POST / PAGE용 PODS 사용자 정의 필드 생성
NUMBER 필드 생성
이름을 page_view로 지정 (대문자 사용 안 함)
2단계:
테마에서 functions.php 파일 찾기 > wp-content \ themes \ your-theme \functions.php
3단계:
아래의 PHP 코드를 사용하세요
<pre data-lang="PHP"><code>function create_view_counter(){
/page_view will be the name of field created in PODS/
$view = get_post_meta(get_the_ID(), 'page_view', true);
/ check if your field is empty /
if ($view == ''){
$view = 0;
}
/auto add 1 view to your counter /
$view = $view+1;
/update your counter back to WP database /
update_post_meta(get_the_ID(), 'page_view', $view);
/display the counter in your page /
return "View: $view";
}
//create the shortcode by the function above
add_shortcode ('viewcounter', 'create_view_counter');</code></pre>
4단계:
게시물 콘텐츠에 쇼트코드를 삽입하세요(삽입하지 않으면 조회수가 집계되지 않으므로 테마나 푸터 등 집계를 원하는 위치에 넣는 것이 좋습니다).
[viewcounter]