我在我的网站上设置了一个很好的投票系统,但由于我想按周、月和所有时间对排名靠前的帖子进行排序。。。我现在需要能够跟踪投票的时间。有人能帮我/推动我朝正确的方向走吗?
我遵循了本教程:
http://wp.tutsplus.com/tutorials/how-to-create-a-simple-post-rating-system-with-wordpress-and-jquery/
JQuery:
jQuery(document).ready(function() {
jQuery(".post-like a").click(function(){
heart = jQuery(this);
// Retrieve post ID from data attribute
post_id = heart.data("post_id");
// Ajax call
jQuery.ajax({
type: "post",
url: ajax_var.url,
data: "action=post-like&nonce="+ajax_var.nonce+"&post_like=&post_id="+post_id,
success: function(count){
// If vote successful
if(count != "already")
{
heart.addClass("voted");
heart.siblings(".count").text(count);
}
}
});
return false;
})
})
函数挂钩。php
add_action(\'wp_ajax_nopriv_post-like\', \'post_like\');
add_action(\'wp_ajax_post-like\', \'post_like\');
类帖子功能:
function post_like()
{
// Check for nonce security
$nonce = $_POST[\'nonce\'];
if ( ! wp_verify_nonce( $nonce, \'ajax-nonce\' ) )
die ( \'Busted!\');
if(isset($_POST[\'post_like\']))
{
// Retrieve user IP address
$ip = $_SERVER[\'REMOTE_ADDR\'];
$post_id = $_POST[\'post_id\'];
// Get voters\'IPs for the current post
$meta_IP = get_post_meta($post_id, "voted_IP");
$voted_IP = $meta_IP[0];
if(!is_array($voted_IP))
$voted_IP = array();
// Get votes count for the current post
$meta_count = get_post_meta($post_id, "votes_count", true);
// Use has already voted ?
if(!hasAlreadyVoted($post_id))
{
$voted_IP[$ip] = time();
// Save IP and increase votes count
update_post_meta($post_id, "voted_IP", $voted_IP);
update_post_meta($post_id, "votes_count", ++$meta_count);
// Display count (ie jQuery return value)
echo $meta_count;
}
else
echo "already";
}
exit;
}
已投票:
function hasAlreadyVoted($post_id)
{
global $timebeforerevote;
// Retrieve post votes IPs
$meta_IP = get_post_meta($post_id, "voted_IP");
$voted_IP = $meta_IP[0];
if(!is_array($voted_IP))
$voted_IP = array();
// Retrieve current user IP
$ip = $_SERVER[\'REMOTE_ADDR\'];
// If user has already voted
if(in_array($ip, array_keys($voted_IP)))
{
$time = $voted_IP[$ip];
$now = time();
// Compare between current time and vote time
if(round(($now - $time) / 60) > $timebeforerevote)
return false;
return true;
}
return false;
}
用我已有的代码,这可行吗?任何帮助或建议都会很棒,非常感谢。