WordPress网站模板纯代码实现 AJAX点赞功能

分  类: wordpress教程

统  计:211 次获取

创建时间: 2024-03-21 14:39:52

  • 详情介绍
13+年专注全栈开发,承接小程序开发,分销系统定制,网站建设、搬家、搭建,二次开发等一站服务,欢迎咨询
wordpress网站里增加访客互动、展示文章热度的时候,除了阅读数、评论数外,还有个有趣的元素,那就是赞,如QQ空间里的说说一样,赞!网上也有很多的点赞插件,大家可以去下载,但是呢插件使用起来有点不方便,也不是不方便就是主题给别人用啊什么的如果有插件还要他们自己装,如果集成代码那么就省心多了。下面就直接给教程吧将下面的代码加入到你的functions.php中就是wordpress主题的函数文件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 localhostsetcookie('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代码$.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 class="favorite&lt;?php if(isset($_COOKIE['bigfa_ding_'.$post-&gt;ID])) echo ' done';?&gt;" data-action="ding" data-id="<?php the_ID(); ?>">喜欢<span class="count"><!--?php if( get_post_meta($post--->ID,'bigfa_ding',true) ){echo get_post_meta($post-&gt;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}希望大家喜欢,哈哈
WordPress网站模板纯代码实现 AJAX点赞功能_wordpress教程 栏目文章由:源码庄整理发布,转载请注明来源。
来源地址:https://www.codenet.cc/article/details/242.html
本站资源未标注正版授权软件均为会员提供或网上搜集,版权归原作者所有,如需商业用途或转载请与原作者联系;
发布的文章及附件仅限用于学习和研究目的,请勿用于商业或违法用途!如有侵权,请及时联系我们!
《更多用户服务协议声明》
  • 波浪
  • 波浪
  • 波浪
  • 波浪