php - 根据 View 更新ID

标签 php wordpress

目前我有以下代码块。

<?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/

相关文章:

php - 在单列上按字母顺序对行数组进行排序,但一个值必须始终位于第一个

wordpress - 使用分类法 ACF 影响自定义分类法帖子计数

wordpress - 如何从子文件夹中忽略根目录中的 .htaccess

php - 通过 PHP 连接到 MySql 数据库句柄

php - 类 XXX 不是有效文档或映射父类(super class)

PHP/MySQL 在表达到一定行数后停止更新

javascript - 在两个 HTML 和 PHP 之间交换变量

具有固定导航栏的 Javascript 不起作用

html - 无法在 Wordpress 中使用字体系列更改字体

wordpress - 服务器配置中URL文件访问被禁用