首页前端开发其他前端知识教你如何在WordPress发布文章时自定义文章作者名称

教你如何在WordPress发布文章时自定义文章作者名称

时间2024-02-10 23:17:02发布访客分类其他前端知识浏览949
导读:收集整理的这篇文章主要介绍了教你如何在WordPress发布文章时自定义文章作者名称,觉得挺不错的,现在分享给大家,也给大家做个参考。 有时候网站会收到一些投稿文章,或者也会转载别人的文...
收集整理的这篇文章主要介绍了教你如何在WordPress发布文章时自定义文章作者名称,觉得挺不错的,现在分享给大家,也给大家做个参考。

有时候网站会收到一些投稿文章,或者也会转载别人的文章,新创建一个用户又有些麻烦,但在作者名称那里显示自己的名字,总不是那么和谐。今天倡萌推荐 @西秦公子 的一个小插件,支持在后台自定义当前文章的作者名称,效果如下图所示:

直接在后台插件安装界面搜索“自定义作者名称”即可在线安装,或者到官方下载:https://litepress.cn/plugins/custom-author/

如果转载或投稿文章比较多,倡萌建议单独创建一个专门用于发布这类文章的用户,然后发布的文章的时候,自定义一下作者名称即可。

下面来看看这个小插件的代码:

?php/*Plugin Name: 	Custom AuthorPlugin URI: 	https://www.ixiqin.COM/2018/06/wordPress-custom-author-plugin/Description: 	自定义作者插件Version: 		1.0Author: 		BestonyAuthor URI: 	https://www.ixiqin.com/License: 		GPL2License URI:  	https://www.gnu.org/licenses/gpl-2.0.htML *//*  Copyright  2018 Bestony (email : xiqingongzi@gmail.com)This PRogram is free Software;
     you can redistribute IT and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation;
     either version 2 of the License, or(at your option) any later version.This program is distributed in the hoPE that it will be useful,but WITHOUT ANY WARRANTY;
     without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program;
     if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */add_action('post_submitbox_misc_actions', 'cus_author_createCustomField');
    add_action('save_post', 'cus_author_saveCustomField');
/** 创建一个checkBox */function cus_author_createCustomField() {
    	$post_id = get_the_ID();
	if (get_post_type($post_id) != 'post') {
    		return;
	}
    	/**	 * 提取现有的值	 * @VAR boolean	 */	$value = get_post_meta($post_id, '_custom_author_name', true);
    	/**	 * 添加 nonce 安全处理	 */	wp_nonce_field('custom_author_nonce' , 'custom_author_nonce');
    	?>
        div class="misc-pub-section misc-pub-section-last dashicons-before dashicons-admin-users">
            label>
    b>
    作者:/b>
    input type="text" value="?php echo $value ?>
    " name="_custom_author_name" />
    /label>
        /div>
    ?php   }
/** * 保存配置信息 * @param  int $post_id 文章的ID */function cus_author_saveCustomField($post_id) {
    	/**	 * 自动保存不处理	 */	if (defined('DOING_AUTOSAVE') &
    &
 DOING_AUTOSAVE) {
    		return;
	}
	/**	 * nonce 信息不正确不处理	 */	if (		!isset($_POST['custom_author_nonce']) ||		!wp_verify_nonce($_POST['custom_author_nonce'], 'custom_author_nonce')	) {
    		return;
	}
	/**	 * 用户无权编辑文章不处理	 */	if (!current_user_can('edit_post', $post_id)) {
    		return;
	}
	/**	 * 存在此项目就更新	 */	if (isset($_POST['_custom_author_name'])) {
    		update_post_meta($post_id, '_custom_author_name', sanitize_text_field($_POST['_custom_author_name']));
	}
 else {
    		/**		 * 不存在就删除		 */		delete_post_meta($post_id, '_custom_author_name');
	}
}
    add_filter('the_author','cus_author_the_author');
function cus_author_the_author($author){
        $custom_author = get_post_meta(get_the_ID(), '_custom_author_name');
    if ($custom_author) {
    		return $custom_author[0];
	}
 else {
    		return $author;
	}
}
    

核心思路就是通过钩子 the_author 来修改了文章作者的显示名称。限定了文章类型为 post(文章),见32行。

至此关于在WordPress发布文章时自定义文章作者名称就结束了,更多关于WordPress技巧与插件请查看下面的相关链接

您可能感兴趣的文章:
  • WordPress中用于获取文章作者与分类信息的方法整理
  • 通过自定义字段重新排序 WordPress 文章方法
  • 禁用WordPress的自动保存草稿文章修订功能方法详解
  • 利用AJAX实现WordPress中的文章列表及评论的分页功能
  • WordPress中设置Post Type自定义文章类型的实例教程
  • 调用WordPress函数统计文章访问量及PHP原生计数器的实现

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 教你如何在WordPress发布文章时自定义文章作者名称
本文地址: https://pptw.com/jishu/609008.html
在 WordPress 的页眉(header)和页脚(footer)添加代码方法 WordPress一键切换为其他用户身份登录功能方法

游客 回复需填写必要信息