WordPress無刷新AJAX點贊功能
WordPress無刷新AJAX點贊功能無插件點贊功能由 #Fatesinger# 提供,不過 @大發(fā) 好像刪除了該篇文章。 實現(xiàn)原理大體是通過自定義字段保存贊數(shù)量,通過Cookie記錄并禁止重復(fù)贊。 具體的代碼如下: function.php /*** AJAX點贊* by:
WordPress無刷新AJAX點贊功能無插件點贊功能由 #Fatesinger# 提供,不過 @大發(fā) 好像刪除了該篇文章。 實現(xiàn)原理大體是通過自定義字段保存贊數(shù)量,通過Cookie記錄并禁止重復(fù)贊。 具體的代碼如下: function.php /*** AJAX點贊* by:
無插件點贊功能由 #Fatesinger# 提供,不過 @大發(fā) 好像刪除了該篇文章。
實現(xiàn)原理大體是通過自定義字段保存贊數(shù)量,通過Cookie記錄并禁止重復(fù)贊。
具體的代碼如下:
/**
* AJAX點贊
* by: #fatesinger.com
* modify: #biji.io #bgbk.org
*/
function dotGood()
{
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);
else update_post_meta($id, 'dotGood', ($specs_raters + 1));
echo get_post_meta($id, 'dotGood', true);
}
die;
}
add_action('wp_ajax_nopriv_dotGood', 'dotGood');
add_action('wp_ajax_dotGood', 'dotGood');
$.fn.postLike = function () {
if ($(this).hasClass('done')) {
alert('點多了傷身體~');
return false;
} else {
$(this).addClass('done');
var id = $(this).data("id"),
action = $(this).data('action'),
rateHolder = $(this).children('.count');
var ajax_data = {
action: "dotGood",
um_id: id,
um_action: action
};
$.post("/wp-admin/admin-ajax.php", ajax_data,
function (data) {
$(rateHolder).html(data);
});
return false;
}
};
$(".dotGood").click(function () {
$(this).postLike();
});
.post-like{margin:10% 0 0;position:relative;}
.post-like a.dotGood{height:30px;line-height:30px;width:30px;font-size:24px;text-align:center;display:inline-block;cursor: pointer;position:relative;}
.post-like a.dotGood.done{color: #e2264d;}
.post-like a.dotGood span{position:absolute;display:inline-block;top:0;left:26px;width:auto;font-size:14px;}
.post-like a.dotGood span:before{content:'+';}
<a href="javascript:;" data-action="topTop" data-id="<?php the_ID(); ?>"
class="dotGood <?php echo isset($_COOKIE['dotGood_' . $post->ID]) ? 'done' : ''; ?>">
♥
<span class="count"><?php echo ($dot_good=get_post_meta($post->ID, 'dotGood', true)) ? $dot_good : '0'; ?></span>
</a>
至此,點贊功能就可以實現(xiàn)了。
版權(quán)所有:深圳市網(wǎng)商在線科技有限公司