非插件实现wordpress文章点赞功能

功能丰富的wordpress点赞插件不少,但对于要在主题中集成简单文章点赞功能的需求,插件就显得不合适,于是乎非插件实现文章点赞功能的方法就诞生,实现思路是:可以通过ajax实时显示点赞数量,自定义字段保存赞数量,Cookies禁止重新点赞。

具体操作步骤。

1、在当前主题functions.php文件中添加代码:

  1. add_action(‘wp_ajax_nopriv_bigfa_like’, ‘bigfa_like’);  
  2. add_action(‘wp_ajax_bigfa_like’, ‘bigfa_like’);  
  3. function bigfa_like(){  
  4.     global $wpdb,$post;  
  5.     $id = $_POST[“um_id”];  
  6.     $action = $_POST[“um_action”];  
  7.     if ( $action == ‘ding’){  
  8.         $bigfa_raters = get_post_meta($id,’bigfa_ding’,true);  
  9.         $expire = time() + 99999999;  
  10.         $domain = ($_SERVER[‘HTTP_HOST’] != ‘localhost’) ? $_SERVER[‘HTTP_HOST’] : false; // make cookies work with localhost  
  11.         setcookie(‘bigfa_ding_’.$id,$id,$expire,’/’,$domain,false);  
  12.         if (!$bigfa_raters || !is_numeric($bigfa_raters)) {  
  13.             update_post_meta($id, ‘bigfa_ding’, 1);  
  14.         }else {  
  15.             update_post_meta($id, ‘bigfa_ding’, ($bigfa_raters + 1));  
  16.         }     
  17.         echo get_post_meta($id,’bigfa_ding’,true);      
  18.     }       
  19.     die;  
  20. }  

2、在主题的header.php文件的</head>前添加以下代码:

  1. <script type=“text/javascript”>  
  2. $(document).ready(function() {   
  3.     $.fn.postLike = function() {  
  4.         if ($(this).hasClass(‘done’)) {  
  5.             alert(‘您已赞过本博客’);  
  6.             return false;  
  7.         } else {  
  8.             $(this).addClass(‘done’);  
  9.             var id = $(this).data(“id”),  
  10.             action = $(this).data(‘action’),  
  11.             rateHolder = $(this).children(‘.count’);  
  12.             var ajax_data = {  
  13.                 action: “bigfa_like”,  
  14.                 um_id: id,  
  15.                 um_action: action  
  16.             };  
  17.             $.post(“<?php bloginfo(‘url’);?>/wp-admin/admin-ajax.php”, ajax_data, function(data) {  
  18.                 $(rateHolder).html(data);  
  19.             });  
  20.             return false;  
  21.         }  
  22.     };  
  23.     $(document).on(“click”“.favorite”function() {  
  24.         $(this).postLike();  
  25.     });  
  26. });   
  27. </script>  

3、在当前主题的single.php文件的<?php the_content();?>代码下面添加点赞按钮调用代码:

  1. <div class=“post-like”>  
  2.     <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”>  
  3.     <?php   
  4.         if( get_post_meta($post->ID,’bigfa_ding’,true) ){              
  5.             echo get_post_meta($post->ID,’bigfa_ding’,true);  
  6.         } else {  
  7.             echo ‘0’;  
  8.         }  
  9.     ?></span>  
  10.     </a>  
  11.  </div>  

4、在当前主题的style.css文件中添加点击按钮样式(仅供参考):

  1. .post-like{text-align:center;padding:10px}  
  2. .post-like a{ background-color:#21759B;border-radius: 3px;color#FFFFFF;font-size12px;padding5px 10px;text-decorationnone;outline:none}  
  3. .post-like a.done, .post-like a:hover{background-color:#eee;color:#21759B;}   
  4. .post-like a.done{cursor:not-allowed}  

注:需要引用版本为1.10或以上的jQuery。

非插件实现wordpress文章点赞功能》有8个想法

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注