来自Fatesinger,文章赞功能随处可见,例如无觅的相关文章工具、百度分享等等。如果想要给主题专门集成点赞功能,还是得需要下面的代码实现。用自定义字段保存赞数量,通过Cookies来禁止重复赞。
加入至Functions.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; }
相对应的JS代码:
$(document).ready(function() { $.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(); }); });
调用代码:
<div class="post-like"> <a href="javascript:;" 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> </div>
参考CSS样式
.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}
点击无效问题解决
请引用版本为1.10或以上的jQuery。
为什么使用无效呢
@购物生活(好店品) 代码应该加错位置了
@购物生活(好店品) 原来是js文件下的$post url地址不正确,我的是本地测试,要在”/wp-admin/admin-ajax.php”加上“.”。改成”./wp-admin/admin-ajax.php”
这个效果可以去36kr官网那,文章的左下角有一个红心标志。
点不到,这个是不是要另外数据库建立表,你是不是漏了什么步骤,效果是有,可是点了后数字依然是0,很明显是数据库的问题。
@百家网络博客 这个和数据库没有一点关系……代码是通过设置cookies保存数据的。
@WP酷 我测试了下正常显示的,原因是jquery版本太低,在1.10下测试正常
@WP酷 我是在本地测试的,有关系否?
@百家网络博客 jQuery版本太低,重新引用个1.10版的
@PC丶爱好者 为什么我是jquery.js?ver=1.11.1的也不行
好像很不错的样子!!!
能不能get一篇文章的喜欢数 ==
@Pete =w=拿来排序都可以
@PC丶爱好者 get_post_meta($id,’bigfa_ding’,true); 搞定
@PC丶爱好者 好了
不错的样子
站长多少评论匡。怎么换的阿。
@阿宇 多说!
@阿宇 搜:36kr 多说
@WP酷 谢谢。
能否帮我看看怎么弄呢,弄了半天也不行 http://pfcz.org/
@破釜沉舟 说是Wordpress自带jquery库没有$方法的事,用jQuery(document).ready(function($)包上或者把$改成jQuery,但是我改了js
也不行
@破釜沉舟 jQuery的版本在1.10以下是不行的,我之后给自己网站加上吧
@blakefan 在主题的functions.php 文件下加入以下代码好像也不行啊,方法不对吗
add_action( ‘init’, ‘jquery_register’ );
function jquery_register() {
if ( !is_admin() ) {
wp_deregister_script( ‘jquery’ );
wp_register_script( ‘jquery’, ( ‘http://cdn.devework.com/js/jquery/1.5.2/jquery.min.js’ ), false, null, true );
wp_enqueue_script( ‘jquery’ );
}
}
@破釜沉舟 ……都说一定要引用1.10版本以上的jQuery了
什么时间在研究下,按你的咋都弄不好,但是这个功能很想要。
已经用上谢谢