添加浏览次数(代码添加,非插件)

日期:2018-11-25 浏览:498次

在function.php底部添加一下代码

function getPostViews( $id ) {
$key = ‘post_views_count’;
$count = get_post_meta( $id, $key, true );
if( $count==” ) {
delete_post_meta( $id, $key );
add_post_meta( $id, $key, ‘0’ );
return “0”;
}
return $count;
}

 

function setPostViews( $id ) {
$key = ‘post_views_count’;
$count = get_post_meta( $id, $key, true );
if( $count==” ) {
$count = 0;
delete_post_meta( $id, $key );
add_post_meta( $id, $key, ‘0’ );
} else {
$count++;
update_post_meta( $id, $key, $count );
}
}

然后在文章(single.php)页面进行调用,其他页面调用一样

<?php setPostViews(get_the_ID());echo getPostViews( get_the_ID() ); ?>

我这里添加一些样式,

<span class=”date” style=”margin-left:10px;”>

浏览:<?php setPostViews(get_the_ID());echo getPostViews( get_the_ID() ); ?>次

</span>

没有评论

发表评论