很好的一天。我一直在研究如何根据
this
链接
但是,当我调用存储在循环中的single.php中的函数时,不管我刷新浏览器多少次,我刚发布的帖子仍然保持0视图(帖子没有更新帖子视图计数)。如果有任何帮助,我将不胜感激。
my functions.php文件-用于后视图计数的代码
function subh_set_post_view($postID)
{
$count_key = 'post_views_count';
$count = (int) get_post_meta($postID, $count_key, true);
if ($count == '') {
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return $count . 'View ';
} else {
$count++;
update_post_meta($postID, $count_key, (string) $count);
}
}
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';
}
/**
* Add a new column in the admin panel posts list
*
* @param $defaults
*
* @return mixed
*/
function subh_posts_column_views($defaults)
{
$defaults['post_views'] = __('Views');
return $defaults;
}
/**
* Display the number of views for each posts on the admin panel
*
* @param $column_name
* @param $id
*
* @return void simply echo out the number of views
*/
function subh_posts_custom_column_views($column_name, $id)
{
if ($column_name === 'post_views') {
echo subh_get_post_view(get_the_ID());
}
}
add_filter('manage_posts_columns', 'subh_posts_column_views');
add_action('manage_posts_custom_column', 'subh_posts_custom_column_views', 5, 2);
我的single.php文件:
<?php
get_header();
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" />
<div id="breadcrumbs" class="stuck_position">
<div class="container">
<div class="row">
<div class="moduletable col-sm-12">
<div class="module_container">
<ul class="breadcrumb">
<li>
<a href="<?php echo esc_url(home_url()); ?>" class="pathway">
Home
</a>
<span class="divider"> / </span>
</li>
<li class="active">
<span>
<?php the_title();?>
</span>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div id="content">
<div class="container">
<div class="row">
<div class="content-inner">
<!-- Left sidebar -->
<div id="component" class="col-sm-12">
<main role="main">
<div id="system-message-container">
</div>
<article class="page-item">
<header class="item_header">
<h3 class="item_title">
<span class="item_title_part_first">
<?php the_title();?>
</span>
</h3>
</header>
<div class="item_info">
<dl class="item_info_dl">
<dd>
<address class="item_createdby">
<?php echo get_the_author(); ?> </address>
</dd>
</dl>
</div>
</div>
</article>
</main>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
if(function_exists('subh_set_post_view')) {
subh_set_post_view(get_the_ID());
}
echo getPostViews(get_the_ID());
?>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer();?>