如何为POST视图自动随机递增

时间:2020-11-24 作者:samanta

i want edit this code to auto increment not just +2 for each user\'s click and instead increment number would be random ( + 2 , + 4 , ... )

if( $should_count && ( ( isset( $views_options[\'use_ajax\'] ) && (int) $views_options[\'use_ajax\'] === 0 ) || ( !defined( \'WP_CACHE\' ) || !WP_CACHE ) ) ) {
                update_post_meta( $id, \'views\', $post_views + 2 );
                do_action( \'postviews_increment_views\', $post_views + 2 );

my full wp postview code bellow:

### Function: Calculate Post Views
add_action( \'wp_head\', \'process_postviews\' );
function process_postviews() {
    global $user_ID, $post;
    if ( is_int( $post ) ) {
        $post = get_post( $post );
    }
    if ( ! wp_is_post_revision( $post ) && ! is_preview() ) {
        if ( is_single() || is_page() ) {
            $id = (int) $post->ID;
            $views_options = get_option( \'views_options\' );
            if ( !$post_views = get_post_meta( $post->ID, \'views\', true ) ) {
                $post_views = 0;
            }
            $should_count = false;
            switch( (int) $views_options[\'count\'] ) {
                case 0:
                    $should_count = true;
                    break;
                case 1:
                    if( empty( $_COOKIE[ USER_COOKIE ] ) && (int) $user_ID === 0 ) {
                        $should_count = true;
                    }
                    break;
                case 2:
                    if( (int) $user_ID > 0 ) {
                        $should_count = true;
                    }
                    break;
            }
            if ( isset( $views_options[\'exclude_bots\'] ) && (int) $views_options[\'exclude_bots\'] === 1 ) {
                $bots = array(
                    \'Google Bot\' => \'google\'
                    , \'MSN\' => \'msnbot\'
                    , \'Alex\' => \'ia_archiver\'
                    , \'Lycos\' => \'lycos\'
                    , \'Ask Jeeves\' => \'jeeves\'
                    , \'Altavista\' => \'scooter\'
                    , \'AllTheWeb\' => \'fast-webcrawler\'
                    , \'Inktomi\' => \'slurp@inktomi\'
                    , \'Turnitin.com\' => \'turnitinbot\'
                    , \'Technorati\' => \'technorati\'
                    , \'Yahoo\' => \'yahoo\'
                    , \'Findexa\' => \'findexa\'
                    , \'NextLinks\' => \'findlinks\'
                    , \'Gais\' => \'gaisbo\'
                    , \'WiseNut\' => \'zyborg\'
                    , \'WhoisSource\' => \'surveybot\'
                    , \'Bloglines\' => \'bloglines\'
                    , \'BlogSearch\' => \'blogsearch\'
                    , \'PubSub\' => \'pubsub\'
                    , \'Syndic8\' => \'syndic8\'
                    , \'RadioUserland\' => \'userland\'
                    , \'Gigabot\' => \'gigabot\'
                    , \'Become.com\' => \'become.com\'
                    , \'Baidu\' => \'baiduspider\'
                    , \'so.com\' => \'360spider\'
                    , \'Sogou\' => \'spider\'
                    , \'soso.com\' => \'sosospider\'
                    , \'Yandex\' => \'yandex\'
                );
                $useragent = isset( $_SERVER[\'HTTP_USER_AGENT\'] ) ? $_SERVER[\'HTTP_USER_AGENT\'] : \'\';
                foreach ( $bots as $name => $lookfor ) {
                    if ( ! empty( $useragent ) && ( false !== stripos( $useragent, $lookfor ) ) ) {
                        $should_count = false;
                        break;
                    }
                }
            }
            $should_count = apply_filters( \'postviews_should_count\', $should_count, $id );
            if( $should_count && ( ( isset( $views_options[\'use_ajax\'] ) && (int) $views_options[\'use_ajax\'] === 0 ) || ( !defined( \'WP_CACHE\' ) || !WP_CACHE ) ) ) {
                update_post_meta( $id, \'views\', $post_views + 2 );
                do_action( \'postviews_increment_views\', $post_views + 2 );
            }
        }
    }
}
1 个回复
SO网友:Rup

要随机添加1-5个视图,可以更改if ( $should_count 在底部阻止:

if( $should_count && ( ( isset( $views_options[\'use_ajax\'] ) && (int) $views_options[\'use_ajax\'] === 0 ) || ( !defined( \'WP_CACHE\' ) || !WP_CACHE ) ) ) {
    $random_increment = rand( 1, 5 );
    update_post_meta( $id, \'views\', $post_views + $random_increment );
    do_action( \'postviews_increment_views\', $post_views + $random_increment );
}
我还是不知道你为什么要这么做。