给WordPress主题分类\标签页面增加点赞功用

择要:起首申明WordPress AJAX 给WordPress主题分类\标签页面增添点赞功用或者是WordPress文章点赞…

起首申明WordPress AJAX 给WordPress主题分类\标签页面增添点赞功用或者是WordPress文章点赞都会给数据库增添表,自WordPress 4.4新增了Term meta,意味着能够像文章点赞一样来完成分类\标签页面增添点赞功用

wordpress今后把下面的代码到场到functions.php:

function wp_term_like( $preifx = null){ global $wp_query; if(!is_tax() && !is_category() && !is_tag()) return ; $tax = $wp_query->get_queried_object(); $id = $tax->term_id; $num = get_term_meta($id,’_term_like’,true) ? get_term_meta($id,’_term_like’,true) : 0; $active = isset($_COOKIE[‘_term_like_’.$id]) ? ‘ is-active’ : ”; $output = ‘<button class=”button termlike’ . $active . ‘” data-action=”termlike” data-action-id=”‘ . $id . ‘”>’ . $prefix . ‘<span class=”count”>’ . $num . ‘</span></button>’; echo $output; } add_action(‘wp_ajax_nopriv_termlike’,’wp_term_like_callback’); add_action(‘wp_ajax_termlike’,’wp_term_like_callback’); function wp_term_like_callback(){ $id = $_POST[‘actionId’]; $num = get_term_meta($id,’_term_like’,true) ? get_term_meta($id,’_term_like’,true) : 0; $domain = ($_SERVER[‘HTTP_HOST’] != ‘localhost’) ? $_SERVER[‘HTTP_HOST’] : false; // make cookies work with localhost setcookie(‘_term_like_’.$id,$id,$expire,’/’,$domain,false); update_term_meta($id,’_term_like’,$num + 1); echo json_encode(array( ‘status’=>200, ‘data’=> $num + 1, )); die; }

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 function wp_term_like ( $ preifx = null ) {      global $ wp_query ;      if ( ! is_tax ( ) && ! is_category ( ) && ! is_tag ( ) ) return ;      $ tax = $ wp_query -> get_queried_object ( ) ;      $ id = $ tax -> term_id ;      $ num = get_term_meta ( $ id , ‘_term_like’ , true ) ? get_term_meta ( $ id , ‘_term_like’ , true ) : 0 ;      $ active = isset ( $ _COOKIE [ ‘_term_like_’ . $ id ] ) ? ‘ is-active’ : ;      $ output = ‘<button class=”button termlike’ . $ active . ‘” data-action=”termlike” data-action-id=”‘ . $ id . ‘”>’ . $ prefix . ‘<span class=”count”>’ . $ num . ‘</span></button>’ ;      echo $ output ; }   add_action ( ‘wp_ajax_nopriv_termlike’ , ‘wp_term_like_callback’ ) ; add_action ( ‘wp_ajax_termlike’ , ‘wp_term_like_callback’ ) ; function wp_term_like_callback ( ) {      $ id = $ _POST [ ‘actionId’ ] ;      $ num = get_term_meta ( $ id , ‘_term_like’ , true ) ? get_term_meta ( $ id , ‘_term_like’ , true ) : 0 ;      $ domain = ( $ _SERVER [ ‘HTTP_HOST’ ] != ‘localhost’ ) ? $ _SERVER [ ‘HTTP_HOST’ ] : false ; // make cookies work with localhost      setcookie ( ‘_term_like_’ . $ id , $ id , $ expire , ‘/’ , $ domain , false ) ;      update_term_meta ( $ id , ‘_term_like’ , $ num + 1 ) ;      echo json_encode ( array (          ‘status’ = > 200 ,          ‘data’ = > $ num + 1 ,          ) ) ;      die ; }

AJAX js代码到场的你的js文件中,注重admin-ajax.php的途径!

jQuery(document).on(“click”, “.termlike”, function() { var _self = jQuery(this); if (_self.hasClass(‘is-active’)) { alert(‘您已赞过啦’) } else { _self.addClass(‘is-active’); jQuery.ajax({ url: /wp-admin/admin-ajax.php,//注重你的该文件途径 data: _self.data(), type: ‘POST’, dataType: “json”, success: function(data) { if (data.status === 200) { _self.find(‘.count’).html(data.data) } else { alert(‘服务器正在勤奋找回自我’) } } }) } });

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 jQuery ( document ) . on ( “click” , “.termlike” , function ( ) {      var _self = jQuery ( this ) ;      if ( _self . hasClass ( ‘is-active’ ) ) {          alert ( ‘您已赞过啦’ )      } else {          _self . addClass ( ‘is-active’ ) ;          jQuery . ajax ( {              url : / wp admin / admin ajax . php , //注重你的该文件途径              data : _self . data ( ) ,              type : ‘POST’ ,              dataType : “json” ,              success : function ( data ) {                  if ( data . status === 200 ) {                      _self . find ( ‘.count’ ) . html ( data . data )                  } else {                      alert ( ‘服务器正在勤奋找回自我’ )                  }              }          } )      } } ) ;

完成以上一切的操纵今后,我们最先讲WordPress分类/标签页面点赞挪用要领:

<?php wp_term_like();?>

1 <?php wp_term_like ( ) ; ?>

在对应归档页面运用下面代码,如在其他地方挪用则不会有任何输出。

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!