我要做的是根本不使用任何挂钩。仅仅因为Wordpress提供了操作和过滤器挂钩,并不意味着您必须使用它们。在页脚中。php文件中,在标记之前添加以下内容。
<?php if ( is_single() ) registerHit(get_the_ID()); ?>
然后在你的函数中。php添加此函数:
function registerHit($post_ID)
{
$meta_key = \'post_views_count\';
$count = get_post_meta($post_ID, $meta_key, true);
if( $count == \'\')
{
$count = 0;
delete_post_meta($post_ID, $meta_key);
add_post_meta($post_ID, $meta_key, \'0\');
}
else
{
$count++;
update_post_meta($post_ID, $meta_key, $count);
}
}
有些人会说,这对Wordpress来说太过手动,但我已经使用Wordpress多年了,我基本上在每个项目上都使用它,有时使用直接向上的函数比使用动作或过滤器更容易。