function k99_post_hits( $id, $action ) {
$k99_HitMetaField = \'_k99_post_hits\'; // Your Custom field that stores the views
$k99_PostHits = get_post_meta($id, $k99_HitMetaField, true);
switch ($action) {
case \'count\' :
if ( $k99_PostHits ==\'\' ) {
$k99_PostHits = mt_rand(200,950); //debug joke
delete_post_meta( $id, $k99_HitMetaField);
add_post_meta( $id, $k99_HitMetaField, $k99_PostHits );
}
$k99_PostHits++;
update_post_meta( $id, $k99_HitMetaField, $k99_PostHits );
break;
case \'display\' :
echo \'this page was viewed: \' , $k99_PostHits ;
}
}
Edit I: neewbie note-忘记解释如何使用:
把这个放在你想要计算的页面中,最好放在循环之外的任何其他内容之前。
<?php k99_post_hits( $post->ID, \'count\' ); //adding post counter?>
将下一行放在要显示计数的位置:
<?php k99_post_hits( $post->ID, \'display\' ); //adding post counter display ?>