functions.php
/**
* AJAX点赞
* by:www.bcoder.cn
*
*/
functiondotGood()
{
global$wpdb,$post;
$id=$_POST["um_id"];
if($_POST["um_action"]=='topTop'){
$specs_raters=get_post_meta($id,'dotGood',true);
$expire=time()+99999999;
$domain=($_SERVER['HTTP_HOST']!='localhost')?$_SERVER['HTTP_HOST']:false;// make cookies work with localhost
setcookie('dotGood_'.$id,$id,$expire,'/',$domain,false);
if(!$specs_raters!is_numeric($specs_raters))update_post_meta($id,'dotGood',1);
elseupdate_post_meta($id,'dotGood',($specs_raters+1));
echoget_post_meta($id,'dotGood',true);
}
die;
}
add_action('wp_ajax_nopriv_dotGood','dotGood');
add_action('wp_ajax_dotGood','dotGood');
/* 将点赞数据挂到后台列表*/
add_filter('manage_posts_columns','add_dotGood_columns');
add_action('manage_posts_custom_column','output_dotGood_columns',10,2);
functionadd_dotGood_columns($columns){
// Add a new field
$columns['zan']=__('点赞');
// Delete an existing field, eg. comments
/* unset( $columns['comments'] );*/
return$columns;
}
functionoutput_dotGood_columns($column_name,$post_id){
switch($column_name){
case"zan":
// Retrieve data and echo result
$zan=get_post_meta($post_id,'dotGood',true);
echo$zan;
break;
}
}
JavaScript
$.fn.postLike=function(){
if($(this).hasClass('done')){
alert('点多了伤身体~');
returnfalse;
}else{
$(this).addClass('done');
varid=$(this).data("id"),
action=$(this).data('action'),
rateHolder=$(this).children('.count');
varajax_data={
action:"dotGood",
um_id:id,
um_action:action
};
$.post("/wp-admin/admin-ajax.php",ajax_data,
function(data){
$(rateHolder).html(data);
});
returnfalse;
}
};
$(".dotGood").click(function(){
$(this).postLike();
});
CSS
post-like{margin:10%;position:relative;}
.post-likea.dotGood{height:30px;line-height:30px;width:30px;font-size:24px;text-align:center;display:inline-block;cursor:pointer;position:relative;}
.post-likea.dotGood.done{color:#e2264d;}
.post-likea.dotGoodspan{position:absolute;display:inline-block;top:;left:26px;width:auto;font-size:14px;}
.post-likea.dotGoodspan:before{content:'+';}
HTML
class="dotGood">
效果图
领取专属 10元无门槛券
私享最新 技术干货