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

我正在破解一些代码,以便让它在一个看不见的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 = '
'; if(hasAlreadyVoted($post_id)) $output .= ' '; else $output .= ' '; $output .= ''.$vote_count.'
'; 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中调用它。