最近在玩WP,觉得很多站的文章栏目,针对作者有个美化,度娘了一下,发现真有好心人提供,所以就转过来,版权归原站长哦

代码如下:
第一步:新建一个文件,命名为:class-wp-widget-authorinfo.php,上传到/wp-includes/widgets里
<?php /* Widget Name:本文作者 Description:显示当前文章的作者信息 Version:1.0 Author:雅兮网 Author URI:https://www.yaxi.net */ add_action('widgets_init', create_function('', 'return register_widget("Authorinfo");')); class Authorinfo extends WP_Widget { function Authorinfo() { $widget_ops = array('description' => '显示当前文章的作者信息!'); $this->WP_Widget('Authorinfo', '本文作者', $widget_ops); } function update($new_instance, $old_instance) { return $new_instance; } function widget($args, $instance) { extract( $args ); echo $before_widget; echo widget_authorinfo(); echo $after_widget; } } function widget_authorinfo(){ ?> <div class="author-info"> <img class="zuozeipc" src="<?php bloginfo('template_url'); ?>/img/post-lz.png"> <div class="author-avatar"> <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ) ?>" title="<?php the_author(); ?>" rel="author"> <?php if (function_exists('get_avatar')) { echo get_avatar( get_the_author_email(),'80' ); }?> </a> </div> <div class="author-name"> <?php the_author_posts_link(); ?> <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ) ?>" target="_blank">(<?php the_author_posts(); ?>篇文章)</a> <span>站长</span> </div> <div class="author-des"> <?php the_author_description(); ?> </div> <div class="author-social"> <span class="author-blog"> <a href="<?php the_author_url(); ?>" rel="nofollow" target="_blank"><i class="icon-home"></i>博客</a> </span> <span class="author-weibo"> <a href="<?php the_author_meta('weibo'); ?>" rel="nofollow" target="_blank"><iclass="icon-weibo"></i>微博</a> </span> </div> </div> <?php } ?>
第二步:将以下CSS代码,加到主题的CSS里
/* 本文作者小工具 */ .author-info{ width: 100%; color: #888; font-size: 12px; background: url(img/author-banner.png) #fff center top no-repeat; position: relative; } .zuozeipc { width: 50px; position: absolute; top: -1px; left: 10px; } .author-avatar{ padding-top: 30px; } .author-avatar a{ display: block; width: 80px; height: 80px; margin: 0 auto; background: #C9C9C9; border-radius: 50%; border: 3px solid #fff; -webkit-border: 3px solid #fff; -moz-border: 3px solid #fff; } .author-avatar .avatar { width: 74px; height: 74px; border-radius: 50%; -webkit-border-radius: 50%; -moz-border-radius: 50%; } .author-name { height: 26px; line-height: 26px; margin: 10px 0; font-weight: bold; font-size: 16px; text-align: center; } .author-name span { font-size: 12px; background: #CECECE; color: #FFFFFF; padding: 2px 6px; margin-left: 5px; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; position: relative; } .author-des { padding: 10px; background: #DFDBDB; text-indent: 2em; } .author-social { text-align: center; padding:20px 10px; } .author-social span{ margin-right: 10px; border-radius: 2px; display: inline-block; } .author-social span:hover { background-color: #1b1b1b; } .author-social span a { padding: 4px 15px; font-size: 14px; color: #fff; } .author-social span a i { margin-right: 5px; } .author-social .author-blog { background-color: #ff5e5c; } .author-social .author-weibo { background-color: #19b5fe; }
第三步:在/wp-includes/找到default-widgets.php
在最下边加上:
/** WP_Widget_Authorinfo class */ require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-authorinfo.php' );
至此,后台已完成,里边涉及的图片,请自行寻找
注意:作者信息后面“站长”二字可以在小工具中自行修改,因为理想情况下笔者是将其展示为当前文章作者的身份信息,如管理员、编辑等等,但遗憾的是笔者目前暂未发现实现办法,就暂且设为固定文字吧。
下半部分两个按钮是调取后台用户个人资料填写的站点和微博,有人会问,为何我的后台资料处没有微博这个选项呢?这个是可以自定义个人信息选项的,只需要在function.php添加如下代码即可,同理可以添加诸如电话、地址等信息;所以本小工具需要自定义一下微博。
//增加个人简介信息 function my_new_contactmethods( $contactmethods ) { $contactmethods['weibo'] = '微博'; return $contactmethods; } add_filter('user_contactmethods','my_new_contactmethods',10,1);
补充:以下代码可以获取帐号的当前角色,替换上一级代码中的站长
< ?php $user_id=get_post($id)->post_author; if(user_can($user_id,'install_plugins')) { echo '管理员'; }elseif(user_can($user_id,'edit_others_posts')) { echo '编辑'; }elseif(user_can($user_id,'publish_posts')) { echo'作者'; }elseif(user_can($user_id,'delete_posts')) { echo'投稿者'; }elseif(user_can($user_id,'read')) { echo'订阅者'; }?>
龙哥 发表于 4年前 (2018-03-07),共3739字
版权声明:①欢迎转载但请注明出处。②如涉及版权联系(izhailong#qq.com)删除!
转载请注明:wordpress新增后台小工具——前台用户 || https://www.izhailong.com/14.html
本文由:版权声明:①欢迎转载但请注明出处。②如涉及版权联系(izhailong#qq.com)删除!
转载请注明:wordpress新增后台小工具——前台用户 || https://www.izhailong.com/14.html
暂无评论