目前我有以下代码块。
<?php
$count = get_tptn_post_count_only($POST_ID);
$rating_class = 'hot3';
if( $count >= 0 && $count <= 3 )
{
$rating_class = 'hot3';
}
elseif( $count > 4 && $count <= 10 )
{
$rating_class = 'hot2';
}
elseif( $count > 11 && $count <= 20 )
{
$rating_class = 'hot1';
}
elseif( $count > 5000 )
{
$rating_class = 'hot1';
}
?>
现在我想做的是跟踪我的帖子上的 View ,然后如果它们超过提供的数量,则相应地更新我的索引页面上的图像,但它似乎不适用于除 hot3 之外的任何内容
部分是第一层。
我目前的所有 View 跟踪都基于一个名为 Top 10 的插件。 。 如果您有兴趣查看我的整个循环,可以这样做 here .
编辑:Plugin Settings .
还有 $totalcntaccess = get_tptn_post_count_only( $id, 'total', $blog_id );
在文件 counter.php 内<-- 另一个 fiddle 链接。
最佳答案
找到了一个修复程序,无需使用上面的插件,而是使用我在一些博客上找到的工具,但使用了类似的方法。
Functions.php ( Source )
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';
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
将其设置在 Single.php 文件的索引中。
<?php
setPostViews(get_the_ID());
?>
然后在你的索引循环中将其设置为这样。
<?php
$count = getPostViews(get_the_ID());
$rating_class = 'hot3';
if( $count >= 0 && $count <= 1000 )
{
$rating_class = 'hot3';
}
elseif( $count > 1000 && $count <= 2500 )
{
$rating_class = 'hot2';
}
elseif( $count > 2500 && $count <= 5000 )
{
$rating_class = 'hot1';
}
elseif( $count > 5000 )
{
$rating_class = 'hot1';
}
print $count;
?>
关于php - 根据 View 更新ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35165999/