把下面這段程式碼加在想顯示文章瀏覽次數的位置(例如模板內的content.php)
<?php setPostViews(get_the_ID()); echo getPostViews(get_the_ID()); ?>
如果需要在列表頁顯示檢視次數,那麼建議在列表頁需要顯示檢視次數的位置單獨新增下面的程式碼
<?php echo getPostViews(get_the_ID()); ?>
把下面這段程式碼加在模板資料夾內的functions.php檔案內
//統計文章瀏覽次數
function getPostViews($postID){
$count_key = "post_views_count";
$count = get_post_meta($postID, $count_key, true);
if($count==""){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, "0");
return "0 檢視";
}
return $count." 檢視";
function setPostViews($postID) {
$count = 0;
}else{
$count++;
update_post_meta($postID, $count_key, $count);
把下面這段程式碼加在想顯示文章瀏覽次數的位置(例如模板內的content.php)
<?php setPostViews(get_the_ID()); echo getPostViews(get_the_ID()); ?>
如果需要在列表頁顯示檢視次數,那麼建議在列表頁需要顯示檢視次數的位置單獨新增下面的程式碼
<?php echo getPostViews(get_the_ID()); ?>
把下面這段程式碼加在模板資料夾內的functions.php檔案內
//統計文章瀏覽次數
function getPostViews($postID){
$count_key = "post_views_count";
$count = get_post_meta($postID, $count_key, true);
if($count==""){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, "0");
return "0 檢視";
}
return $count." 檢視";
}
function setPostViews($postID) {
$count_key = "post_views_count";
$count = get_post_meta($postID, $count_key, true);
if($count==""){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, "0");
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}