通过随机数更改功能中的帖子视图数

时间:2016-06-03 17:28:48

标签: wordpress post-meta

我在wordpress主题中有这个功能:

// function to count views.
function setPostViews_anthemes($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);
    }
}    

我需要viws的数量显示从1到999的随机数

1 个答案:

答案 0 :(得分:1)

要在PHP中生成从1999的随机整数,请使用rand(1,999)

// function to count views.
function setPostViews_anthemes($postID) {
    $count_key = 'post_views_count';
    $count = rand(1,999);   
    update_post_meta($postID, $count_key, $count);
}