WordPress 文章实现思路 Like 函数代码方法
实现思路
通过 ajax 实时显示点赞,将点赞保存在自定义字段中,并且 cookies 禁用重新点赞。
添加代码
将以下代码添加到theme.php
:
add_action('wp_ajax_nopriv_bigfa_like', 'bigfa_like');
add_action('wp_ajax_bigfa_like', 'bigfa_like');
function bigfa_like(){
global $wpdb,$post;
$id = $_POST["um_id"];
$action = $_POST["um_action"];
if ( $action == 'ding'){
$bigfa_raters = get_post_meta($id,'bigfa_ding',true);
$expire = time() + 99999999;
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhost
setcookie('bigfa_ding_'.$id,$id,$expire,'/',$domain,false);
if (!$bigfa_raters || !is_numeric($bigfa_raters)) {
update_post_meta($id, 'bigfa_ding', 1);
}
else {
update_post_meta($id, 'bigfa_ding', ($bigfa_raters + 1));
}
echo get_post_meta($id,'bigfa_ding',true);
}
die;
}
将以下代码添加到❙将以下代码添加到❙
add_action('wp_ajax_nopriv_bigfa_like', 'bigfa_like');
add_action('wp_ajax_bigfa_like', 'bigfa_like');
function bigfa_like(){
global $wpdb,$post;
$id = $_POST["um_id"];
$action = $_POST["um_action"];
if ( $action == 'ding'){
$bigfa_raters = get_post_meta($id,'bigfa_ding',true);
$expire = time() + 99999999;
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhost
setcookie('bigfa_ding_'.$id,$id,$expire,'/',$domain,false);
if (!$bigfa_raters || !is_numeric($bigfa_raters)) {
update_post_meta($id, 'bigfa_ding', 1);
}
else {
update_post_meta($id, 'bigfa_ding', ($bigfa_raters + 1));
}
echo get_post_meta($id,'bigfa_ding',true);
}
die;
}
主题❙ 时代,所以请确保您事先安装了jQuery,否则将无法正常工作。
<script type="text/javascript">
$.fn.postLike = function() {
if ($(this).hasClass('done')) {
return false;
} else {
$(this).addClass('done');
var id = $(this).data("id"),
action = $(this).data('action'),
rateHolder = $(this).children('.count');
var ajax_data = {
action: "bigfa_like",
um_id: id,
um_action: action
};
$.post("/wp-admin/admin-ajax.php", ajax_data,
function(data) {
$(rateHolder).html(data);
});
return false;
}
};
$(document).on("click", ".favorite",
function() {
$(this).postLike();
});
</script>
编辑文章页面single.php
并在需要的地方添加“赞”按钮。代码如下:
<a href="javascript:;" rel="external nofollow" target = "_blank" rel="external nofollow" target = "_blank" data-action="ding" data-id="<?php the_ID(); ?>" class="favorite<?php if(isset($_COOKIE['bigfa_ding_'.$post->ID])) echo ' done';?>">喜欢 <span class="count"> <?php if( get_post_meta($post->ID,'bigfa_ding',true) ){ echo get_post_meta($post->ID,'bigfa_ding',true); } else { echo '0'; }?> </span> </a>
通过以上三段代码实现了类似文章的功能,但是样式效果很糟糕。 作者还提供了以下添加到主题中的样式style.css
(xiaoz下的样式没有测试)
.post-like{text-align:center;padding:10px}
.post-like a{ background-color:#21759B;border-radius: 3px;color: #FFFFFF;font-size: 12px;padding: 5px 10px;text-decoration: none;outline:none}
.post-like a.done, .post-like a:hover{background-color:#eee;color:#21759B;}
.post-like a.done{cursor:not-allowed}
但是每个主题的样式可能不同,上面的CSS样式不是必然相同。这是合适的。建议大家根据自己的主题风格来写CSS样式(需要一定的前端基础)
改进
上面的方法是用PHP判断是否存在COOKIE,然后更改class属性,以及然后判断按钮是否可以点击。但是,如果您的网站启用了 CDN 或使用静态缓存插件(如 WP-Super-Cache
),该页面将被预先缓存,因此判决将无效,您可以喜欢它对我来说无限制,所以xiaoz做了改进,用js来判断COOKIES是否又存在。代码如下。
将以下代码添加到主题页面single.php
:
<a id = "praise" data-no-instant class="favorite<?php if(isset($_COOKIE['bigfa_ding_'.$post->ID])) echo ' done';?>" href="javascript:;" rel="external nofollow" target = "_blank" rel="external nofollow" target = "_blank" data-action="ding" data-id="<?php the_ID(); ?>"><i class="fa fa-thumbs-o-up"></i> 赞 <span class="count"> <?php if( get_post_meta($post->ID,'bigfa_ding',true) ){ echo get_post_meta($post->ID,'bigfa_ding',true); } else { echo '0'; }?> </span> </a>
将以下代码添加到主题页面❙pendfooter.php的底部。 on jQuery):
<script type="text/javascript">
//获取cookie
function getCookie(cookieName){
var cookieValue="";
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
if (cookie.substring(0, cookieName.length + 2).trim() == cookieName.trim() + "=") {
cookieValue = cookie.substring(cookieName.length + 2, cookie.length);
break;
}
}
}
return cookieValue;
}
$.fn.postLike = function() {
if ($(this).hasClass('done')) {
return false;
} else {
$(this).addClass('done');
var id = $(this).data("id"),
action = $(this).data('action'),
rateHolder = $(this).children('.count');
var ajax_data = {
action: "bigfa_like",
um_id: id,
um_action: action
};
$.post("/wp-admin/admin-ajax.php", ajax_data,
function(data) {
$(rateHolder).html(data);
$("#praise").removeClass("layui-btn-danger").addClass("layui-bg-gray");
});
return false;
}
};
$(document).on("click", ".favorite",
function() {
var post_id = $("#praise").attr("data-id");
//获取COOKIE
if ( getCookie('bigfa_ding_' + post_id) != '' ){
alert('您已经点过赞了!);
}
else{
$(this).postLike();
}
});
</script>
总结
本文主要提供代码思路和实现。其实你需要根据自己的情况修改和调整代码,这对于开发者来说应该不难。
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。