WordPress如何实现用户关注功能?

最近很多用户问我,能否在WordPress主题中添加一个用户关注功能,答案是肯定的,那么WordPress如何…

WordPress如何实现用户关注功能?WordPress开发教程 第1张

最近很多用户问我,能否在WordPress主题中添加一个用户关注功能,答案是肯定的,那么WordPress如何实现用户关注功能?首先,将下面的代码放到您的functions.php文件中。

function wpdiary_user_guanzhu(){

if(!get_current_user_id()){

exit(json_encode([‘msg’=>’请登录!’]));

}

if( $_POST[‘user’]==get_current_user_id()){

exit(json_encode([‘msg’=>’你不能关注自己!’]));

}

$user = $_POST[‘user’];

$meta = get_user_meta(get_current_user_id(),’guanzhu’,true);

$bguanzhu1 = explode(‘,’,get_user_meta($user,’bguanzhu’,true));

$bguanzhu = array_filter($bguanzhu1);

$guanzhu1 = explode(‘,’,get_user_meta(get_current_user_id(),’guanzhu’,true));

$guanzhu = array_filter($guanzhu1);

if(in_array($user,$guanzhu)){

foreach($guanzhu as $k=>$v){

if($v==$user){

unset($guanzhu[$k]);

}

}

foreach($bguanzhu as $k=>$v){

if($v==get_current_user_id()){

unset($bguanzhu[$k]);

}

}

update_user_meta(get_current_user_id(),’guanzhu’,implode(“,”,$guanzhu));

update_user_meta($user,’bguanzhu’,implode(“,”,$bguanzhu));

exit(json_encode([‘msg’=>’取消成功!’]));

}else{

array_push($guanzhu,$user);

array_push($bguanzhu,get_current_user_id());

update_user_meta(get_current_user_id(),’guanzhu’,implode(“,”,$guanzhu));

update_user_meta($user,’bguanzhu’,implode(“,”,$bguanzhu));

exit(json_encode([‘msg’=>’关注成功!’]));

}

}

add_action(‘wp_ajax_guanzhu’,’wpdiary_user_guanzhu’);

add_action(‘wp_ajax_nopriv_guanzhu’,’wpdiary_user_guanzhu’);

//获取指定用户关注数量

function get_wpdiary_guanzhu_count($authorID){

$meta = get_user_meta($authorID,’guanzhu’,true);

if($meta){

$guanzhu = explode(“,”,get_user_meta($authorID,’guanzhu’,true));

return count($guanzhu);

}else{

return 0;

}

}

//获取指定用户被关注数量

function get_wpdiary_bguanzhu_count($authorID){

$meta = get_user_meta($authorID,’bguanzhu’,true);

if($meta){

$bguanzhu = explode(“,”,get_user_meta($authorID,’bguanzhu’,true));

return count($bguanzhu);

}else{

return 0;

}

}

其次,消息提示使用了layer这个jQuery弹出层插件,所以,您还需要在主题中引入这个框架的文件(下载地址:http://res.layui.com/static/download/layer/layer-v3.1.1.zip),下载后把整个文件夹放到您的主题目录中,然后在主题头部引入layer.js,并将下面的JS代码放到您的footer.php中。

<script>

$(‘a.addfollow’).click(function(){

var user = $(this).attr(‘user’);

$.ajax({

url: “/wp-admin/admin-ajax.php”,

type:’post’,

dataType:’json’,

data:{action:’guanzhu’,user:user},

success: function(data){

layer.alert(data.msg, function(){

location.reload();

});

}

});

});

</script>

第三,将关注按钮放到您需要放的地方,比如用户列表中或作者存档页,关注按钮代码如下:

<?php

$meta = get_user_meta(get_current_user_id(),’guanzhu’,true);

$user = ‘要被关注的用户的ID’

if($meta){

$guanzhu = explode(‘,’,$meta);

}else{

$guanzhu = array();

}

if(in_array($user,$guanzhu)){?>

<a href=”javascript:;” user=”<?php echo $user;?>” class=”addfollow focus-link”>+取消关注</a>

<?php }else{?>

<a href=”javascript:;” user=”<?php echo $user;?>” class=”addfollow focus-link”>+ 关注</a>

<?php }?>

第四,获取指定用户关注和被关注数量:

<?php echo get_wpdiary_guanzhu_count(‘用户ID’);?>//获取指定用户关注数量

<?php echo get_wpdiary_bguanzhu_count(‘用户ID’);?>//获取指定用户被关注数量

第五,如何获取当前登录用户关注的用户的列表:

<?php

$user_id = get_current_user_id();//当前登录用户id,也可以指定为某个ID的用户,这样获取到的就是指定ID的用户的关注列表

$meta = get_user_meta($user_id,’guanzhu’,true);

$guanzhulist = explode(‘,’,$meta );

$users = get_users(array(‘include’=>$guanzhulist));

?>

<?php foreach($users as $user){ ?>

<li class=”pic-btn-li”>

<div class=”li-pic”>

<a href=”<?php%20echo%20get_author_posts_url(%20$user->ID);%20?>/?userid=<?php%20echo%20$user->ID;?>” target=”_blank” class=”li-link”>

<?php echo get_avatar($user->ID);?>

</a>

</div>

<div class=”li-dec”>

<p class=”dec-name”>

<a href=”<?php%20echo%20get_author_posts_url(%20$user->ID);%20?>/?userid=<?php%20echo%20$user->ID;?>” target=”_blank” class=”dec-name-link”><?php echo $user->nickname;?></a>

</p>

<p class=”dec-sub”>

<span class=”dec-fans”>粉丝 <?php echo get_wpdiary_bguanzhu_count($user->ID);?></span>

</p>

</div>

<a href=”javascript:;” user=”<?php echo $user->ID;?>” class=”addfollow sp-mod-button” title=”关注他”>取消</a>

</li>

<?php } ?>

第六,获取关注过当前登录用户的列表(被关注列表,也可以说是粉丝列表):

<?php

$user_id = get_current_user_id();//当前登录用户id,也可以指定为某个ID的用户,这样获取到的就是关注过指定ID的用户列表(粉丝列表)

$bguanzhu = get_user_meta($user_id,’bguanzhu’,true);

$fanslist = explode(‘,’,$bguanzhu );

$fans = get_users(array(‘include’=>$fanslist));

?>

<?php foreach($fans as $fensi){ ?>

<li class=”pic-btn-li”>

<div class=”li-pic”>

<a href=”<?php%20echo%20get_author_posts_url(%20$fensi->ID);%20?>/?userid=<?php%20echo%20$fensi->ID;?>” target=”_blank” class=”li-link”>

<?php echo get_avatar($fensi->ID);?>

</a>

</div>

<div class=”li-dec”>

<p class=”dec-name”>

<a href=”<?php%20echo%20get_author_posts_url(%20$fensi->ID);%20?>/?userid=<?php%20echo%20$fensi->ID;?>” target=”_blank” class=”dec-name-link”><?php echo $fensi->nickname;?></a>

</p>

<p class=”dec-sub”>

<span class=”dec-fans”>粉丝 <?php echo get_wpdiary_bguanzhu_count($fensi->ID);?></span>

</p>

</div>

<?php

$meta = get_user_meta(get_current_user_id(),’guanzhu’,true);

$user = $fensi->ID;

if($meta){

$guanzhu = explode(‘,’,get_user_meta(get_current_user_id(),’guanzhu’,true));

}else{

$guanzhu = array();

}

if(in_array($user,$guanzhu)){?>

<a href=”javascript:;” user=”<?php echo $fensi->ID;?>” class=”addfollow sp-mod-button” title=”取消关注”>已互关</a>

<?php }else{?>

<a href=”javascript:;” user=”<?php echo $fensi->ID;?>” class=”addfollow sp-mod-button” title=”关注他”>关注他</a>

<?php }?>

</li>

<?php } ?>
类别:WordPress入门

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册