langkah pertama yang harus kita lakukan ialah.
1. Buka file functions.php Anda dan masukkan kode di bawah ini ke dalamnya dan letakkan sesudah tag php (<?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 View”;
}
return $count.’ Views’;
}
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);
}
}
2. Buka file single.php dan tambahkan kode berikut :
<?php setPostViews(get_the_ID()); ?>
Perhatikan gambar di bawah. Dalam gambar tersebut Anda dapat melihat Saya memasukkan code sesudah while(have_posts().
Anda masih berada file di single.php. Kode pada langkah kedua hanya untuk menginisialisasi fungsi dalam file single.php. Sekarang Anda perlu menampilkan jumlah posting dilihat atau dibaca. Untuk menampilkan jumlah posting dibaca gunakan kode di bawah ini:
<?php echo getPostViews(get_the_ID()); ?>Saya meletakkan kode tersebut di bawah judul bersebelahan dengan tanggal dan kategori. Anda dapat meletakkannya di bawah posting, misalnya.
<div><h1><?php the_title(); ?></h1></div>
<div><?php the_time(‘l, F jS Y’) ?> | <?php the_category(‘, ‘) ?> | Telah dibaca <?php echo getPostViews(get_the_ID()); ?></div>
<?php the_content(); ?>
Dan terakhir jangan lupa untuk “Update” dan hasilnya akan seperti :

Tidak ada komentar:
Posting Komentar