WordPress文章显示修订者而非原创作者的问题解决问询
WordPress 6.8.1 修订文章后作者显示异常问题
使用WordPress 6.8.1版本,我通过user1创建了一篇文章,之后user2对该文章进行了修订。现在文章下方显示的作者是user2,而非原创作者user1。
我尝试编辑author-box.php文件,但发现代码返回的是修订者信息:
global $post; $post_id = $post->ID; $post_author_id = get_post_field('post_author', $post_id);
$post_author_id的值为修订文章的作者ID,而非原创作者ID。
我有以下几个问题:
- WordPress默认设置是否会显示最后修订文章的作者?
- 是否有简便配置可固定显示原创作者,不受修订操作影响?
- 若没有,如何通过代码实现该需求?
author-box.php完整代码
<?php $epcl_theme = epcl_get_theme_options(); ?> <?php if( !get_the_author_meta('description') ) return; $author_id = get_the_author_meta('ID'); $user_meta = get_user_meta( $author_id, 'epcl_user', true ); $author_avatar = epcl_get_author_avatar($user_meta, $author_id); $author_name = get_the_author(); $author_url = get_author_posts_url($author_id); $class = $author_position = ''; if($author_avatar) $class .= ' with-avatar'; else $class .= ' no-avatar'; if( !empty($user_meta) && !empty( $user_meta['position']) ){ $author_position = $user_meta['position']; } $website = get_the_author_meta('user_url'); if( is_author() ) $class .= ' grid-container'; // My code here global $post; $post_id = $post->ID; $post_author_id = get_post_field('post_author', $post_id); ?> <!-- start: .author --> <div> <?php echo $post_author_id; ?> </div> <section id="author" class="author section <?php echo esc_attr($author_name); ?> <?php echo esc_attr($class); ?>"> <div class="flex"> <a href="<?php echo get_author_posts_url($author_id); ?>" title="<?php echo esc_attr__('Author3:', 'maktub').' '.esc_attr($author_name); ?>" class="author meta-info epcl-dropcap rounded large no-border <?php if($author_avatar) echo 'epcl-loader'; ?>"> <?php if($author_avatar): ?> <?php if( epcl_is_amp() ): ?> <span class="author-image cover" style="background-image: url('<?php echo esc_url($author_avatar); ?>');"></span> <?php else: ?> <span class="author-image cover lazy" data-src="<?php echo esc_url($author_avatar); ?>"></span> <?php endif; ?> <?php else: ?> <span><?php echo mb_substr( $author_name, 0, 1); ?></span> <?php endif; ?> </a> <div class="right"> <h4 class="title author-name underline-effect"> <?php if( is_archive() ): ?> <?php echo esc_html( $author_name ); ?> <?php else: ?> <a href="<?php echo esc_attr( $author_url ); ?>"><?php echo esc_html( $author_name ); ?></a> <?php endif; ?> </h4> <p class="position"><?php echo esc_html($author_position); ?></p> </div> </div> <div class="clear"></div> <!-- start: .info --> <div class="info"> <p><?php the_author_meta('description'); ?></p> <?php if( !empty($user_meta) && ($user_meta['facebook'] || $user_meta['twitter'] || $website) ): ?> <div class="social"> <?php if( $user_meta['twitter'] ): ?> <a href="<?php echo esc_url( $user_meta['twitter'] ); ?>" class="twitter tooltip" title="<?php esc_attr_e('Follow me on Twitter', 'maktub'); ?>" target="_blank" rel="nofollow noopener" aria-label="<?php esc_attr_e('Follow me on Twitter', 'maktub'); ?>"><svg class="icon large"><use xlink:href="#twitter-icon"></use></svg></a> <?php endif; ?> <?php if( $user_meta['facebook'] ): ?> <a href="<?php echo esc_url( $user_meta['facebook'] ); ?>" class="facebook tooltip" title="<?php esc_attr_e('Follow me on Facebook', 'maktub'); ?>" target="_blank" rel="nofollow noopener" aria-label="<?php esc_attr_e('Follow me on Facebook', 'maktub'); ?>"><i class="fa fa-facebook"></i></a> <?php endif; ?> <?php if( $website ): ?> <a href="<?php echo esc_url( $website ); ?>" class="website tooltip" title="<?php esc_attr_e('Website', 'maktub'); ?>: <?php echo esc_url( $website ); ?>" target="_blank" rel="nofollow noopener" aria-label="<?php esc_attr_e('Website', 'maktub'); ?>: <?php echo esc_url( $website ); ?>"><i class="fa fa-globe"></i></a> <?php endif; ?> </div> <?php endif; ?> <div class="clear"></div> <?php if( is_single() ): ?> <a href="<?php echo esc_url( $author_url ); ?>" class="epcl-button black bordered" data-title="<?php esc_attr_e('View All Articles', 'maktub'); ?>"><?php esc_html_e('View All Articles', 'maktub'); ?></a> <?php endif; ?> </div> <!-- end: .info --> <div class="clear"></div> </section> <!-- end: .author --> <div class="clear"></div>
single.php中引用代码
<?php if( epcl_get_option('enable_single_author', true) ): ?> <?php get_template_part('partials/author-box'); ?> <?php endif; ?>
更新2
编辑文章时,数据库中会新增一条post_type为revision的记录

访问修订后的文章时,显示的post_author为修订者ID(截图中post_author为18是原创作者,1是修订者)
问题解答
1. WordPress默认设置是否会显示最后修订文章的作者?
WordPress核心默认逻辑中,修订操作不会修改原文章的post_author字段——原文章作者始终是创建者,只有修订记录的post_author会标记为修订者。你遇到的情况大概率是主题或第三方插件修改了这一默认行为,导致原文章作者被替换成了修订者。
2. 是否有简便配置可固定显示原创作者?
WordPress核心没有直接的开关选项,但可以先检查当前主题的设置面板,看是否有“作者显示优先级”之类的配置项。如果主题未提供相关设置,就需要通过代码实现。
3. 代码实现方案
方案一:强制获取原创作者(即使原文章作者被修改)
在author-box.php中替换原有的作者获取逻辑,从修订记录中追溯原始创建者:
global $post; $post_id = $post->ID; // 获取所有修订记录,按创建时间升序排列 $revisions = wp_get_post_revisions($post_id, array('order' => 'ASC')); if (!empty($revisions)) { // 第一条修订记录就是文章的原始版本 $first_revision = reset($revisions); $original_author_id = get_post_field('post_author', $first_revision->ID); } else { // 无修订记录,直接使用当前文章作者 $original_author_id = get_post_field('post_author', $post_id); } // 替换原有作者变量,后续所有作者信息都基于原创作者ID $author_id = $original_author_id; $user_meta = get_user_meta( $author_id, 'epcl_user', true ); $author_avatar = epcl_get_author_avatar($user_meta, $author_id); $author_name = get_the_author_meta('display_name', $author_id); $author_url = get_author_posts_url($author_id); $website = get_the_author_meta('user_url', $author_id);
方案二:阻止修订时修改原文章作者
如果是修订操作错误覆盖了原文章的post_author,可以在主题的functions.php中添加钩子拦截:
add_action('wp_insert_post', function($post_id, $post, $update) { // 仅处理文章类型,且是更新操作(非新建) if ($post->post_type !== 'post' || !$update || wp_is_post_revision($post_id)) { return; } // 获取文章原始作者 $original_author = get_post_field('post_author', $post_id); // 若当前保存的作者与原始作者不一致,强制改回 if ($post->post_author != $original_author) { wp_update_post(array( 'ID' => $post_id, 'post_author' => $original_author )); } }, 10, 3);
内容的提问来源于stack exchange,提问作者Anuj TBE
相关产品推荐
相关产品推荐

