很多站长朋友想要在WordPress主题分类\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\标签页面增加点赞功能,那么如何为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;

}

创客主机

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(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’服务器正在努力找回自我\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’)

}

}

})

}

});

完成以上所有的操作以后,我们开始讲 WordPress 分类/标签页面点赞调用方法:

<?php wp_term_like();?>

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

相关新闻

发表评论

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

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部