使用jQuery计算点击次数并使用Ajax显示

时间:2013-08-28 01:46:03

标签: javascript jquery mysql ajax wordpress

我正在破解某些代码,以便让它在一个不可见的div元素上注册一个点击,这会扩展一篇文章以显示它的摘录,同时为任何人点击它的时间加上+1。之后,它会使用ajax更新一个元素,其中包含收到的点击次数。

至少,这就是目标。

以下代码最终打破了Wordpress并给了我白色的厄运屏幕。这是从带有Ajax回调的简单点击计数器中获取的,以更新数字。

我的问题所在,就是为了记录不同元素的点击次数。

不浪费任何时间,这是我的问题:

我不是只需要将所有post_like重命名为post_reader吗?有人一直在亲自告诉我should work so check your server,但这似乎很荒谬......

注意,在您看到post_reader的位置之后,它之前已经说过post_like

// post click to expand button

$timebeforerevote = 1;

add_action('wp_ajax_nopriv_post-like', 'post_reader');
add_action('wp_ajax_post-like', 'post_reader');

wp_localize_script('like_post', 'ajax_var', array(
    'url' => admin_url('admin-ajax.php'),
    'nonce' => wp_create_nonce('ajax-nonce')
));

function post_like()
{
    $nonce = $_POST['nonce'];

    if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
        die ( 'Busted!');

    if(isset($_POST['post_reader']))
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        $post_id = $_POST['post_id'];

        $meta_IP = get_post_meta($post_id, "voted_IP");

        $voted_IP = $meta_IP[0];
        if(!is_array($voted_IP))
            $voted_IP = array();

        $meta_count = get_post_meta($post_id, "votes_count", true);

        if(!hasAlreadyVoted($post_id))
        {
            $voted_IP[$ip] = time();

            update_post_meta($post_id, "voted_IP", $voted_IP);
            update_post_meta($post_id, "votes_count", ++$meta_count);

            echo $meta_count;
        }
        else
            echo "already";
    }
    exit;
}

function hasAlreadyVoted($post_id)
{
    global $timebeforerevote;

    $meta_IP = get_post_meta($post_id, "voted_IP");
    $voted_IP = $meta_IP[0];
    if(!is_array($voted_IP))
        $voted_IP = array();
    $ip = $_SERVER['REMOTE_ADDR'];

    if(in_array($ip, array_keys($voted_IP)))
    {
        $time = $voted_IP[$ip];
        $now = time();

        if(round(($now - $time) / 60) > $timebeforerevote)
            return false;

        return true;
    }

    return false;
}

function getPostReadLink($post_id)
{
    $themename = "toolbox";

    $vote_count = get_post_meta($post_id, "votes_count", true);

    $output = '<div class="post-read">';
    if(hasAlreadyVoted($post_id))
        $output .= ' <span title="'.__('I like this article', $themename).'" class="qtip like alreadyvoted"></span>';
    else
        $output .= '<a href="#" data-post_id="'.$post_id.'">
                    <span  title="'.__('I like this article', $themename).'"class="qtip like"></span>
                </a>';
    $output .= '<span class="count">'.$vote_count.'</span></div>';

    return $output;
}

点击时调用的函数:

jQuery(".expand").click(function(e){

    e.preventDefault();

    readers = jQuery(this);

    // Retrieve post ID from data attribute
    post_id = readers.data("post_id");

    // Ajax call
    jQuery.ajax({
        type: "post",
        url: ajax_var.url,
        data: "action=post-reader&nonce="+ajax_var.nonce+"&post_reader=&post_id="+post_id,
        success: function(count){
            // If vote successful
            if(count != "already")
            {
                heart.addClass("readered");
                heart.siblings(".count").text(count);
            }
        }
    });

    return false;
})

在适当的div中调用它。

<?php echo getPostReadLink(get_the_ID());?>

0 个答案:

没有答案