wordpress扩展个人资料增加微博微信

WordPress教程 1668

wordpress用户个人资料供设置的信息太少,如果想自定义增加一些其它的个人资料信息怎么办?wordpress提供了user_contactmethods过滤器通过add_filter()函数让用户扩展个人资料设置,且操作非常简单,下面博客吧以添加微博微信为例说明实现步骤。

操作步骤:

1、在当前主题的functions.php文件第一行的<?php下面添加以下代码:

1
2
3
4
5
6
function extended_profile($contactmethods) {
	$contactmethods['weibo'] = '新浪微博URL';
	$contactmethods['weixin'] = '微信图片URL';	
	return $contactmethods;
}
add_filter('user_contactmethods','extended_profile');

2、在主题的single.php文件中添加调用代码:

调用新浪微博

1
2
3
<?php if (get_the_author_meta('weibo')){ ?>
<a href="<?php the_author_meta('weibo');?>" target="_blank" title="新浪微博">新浪微博</a>
<?php } ?>

调用微信二维码

1
2
3
<?php if (get_the_author_meta('weixin')){ ?>
<img src="<?php the_author_meta('weixin');?>" alt="扫描二维码关注我们"/>
<?php } ?>
扩展:

如果要在文章内容页面以外的地方调用,可以使用以下代码:

1
<?php echo get_the_author_meta('weibo','1');?>

把代码中的1换成作者ID即可。

精品推荐: