我是PHP / Wordpress的新手,所以如果我错了请帮助我。
我已将 functions.php 的一些代码添加到计数,然后显示帖子视图。它在下面:
function getPostViews($postID, $is_single = true){
global $post;
if(!$postID) $postID = $post->ID;
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if(!$is_single){
return '<span class="svl_show_count_only">'.$count.' Views</span>';
}
$nonce = wp_create_nonce('devvn_count_post');
if($count == "0"){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '330');
return '<span class="svl_post_view_count" data-id="'.$postID.'" data-nonce="'.$nonce.'">0 Views</span>';
}
return '<span class="svl_post_view_count" data-id="'.$postID.'" data-nonce="'.$nonce.'">'.$count.' Views</span>';
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count == "0" || empty($count) || !isset($count)){
add_post_meta($postID, $count_key, '0');
update_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
add_action( 'wp_ajax_svl-ajax-counter', 'svl_ajax_callback' );
add_action( 'wp_ajax_nopriv_svl-ajax-counter', 'svl_ajax_callback' );
function svl_ajax_callback() {
if ( !wp_verify_nonce( $_REQUEST['nonce'], "devvn_count_post")) {
exit();
}
$count = 0;
if ( isset( $_GET['p'] ) ) {
global $post;
$postID = intval($_GET['p']);
$post = get_post( $postID );
if($post && !empty($post) && !is_wp_error($post)){
setPostViews($post->ID);
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
}
}
die($count.' lượt xem bài viết');
}
add_action( 'wp_footer', 'svl_ajax_script', PHP_INT_MAX );
function svl_ajax_script() {
if(!is_single()) return;
?>
<script>
(function($){
$(document).ready( function() {
$('.svl_post_view_count').each( function( i ) {
var $id = $(this).data('id');
var $nonce = $(this).data('nonce');
var t = this;
$.get('<?php echo admin_url( 'admin-ajax.php' ); ?>?action=svl-ajax-counter&nonce='+$nonce+'&p='+$id, function( html ) {
$(t).html( html );
});
});
});
})(jQuery);
</script>
<?php
}
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);
function posts_column_views($defaults){
$defaults['post_views'] = __( ' Views' , '' );
return $defaults;
}
function posts_custom_column_views($column_name, $id){
if( $column_name === 'post_views' ) {
echo getPostViews( get_the_ID(), false);
}
}
然后将其添加到 single.php :
<?php echo setPostViews(get_the_ID()); ?>
<?php echo getPostViews(get_the_ID()); ?>
到目前为止一直运作良好,一些帖子观看次数超过7000次。这些帖子向我展示了“阵列视图”,而其他人仍然可以。
我很难找到原因,但我正在努力。我该怎么办?