theme_entry_meta在single_*.php失效,分类/搜索页正常的技术求助
解决theme_entry_meta()在single模板无输出的问题
核心问题分析
从排查结果和代码来看,问题出在single模板中调用函数时,当前Post对象的上下文未正确绑定到函数内部,导致theme_entry_meta()依赖的辅助函数(如theme_the_author、theme_the_date)无法获取有效Post ID。原函数中$show_comments_first的!is_singular()判断仅影响评论位置,不是无输出的直接原因。
具体修复方案
方案1:修复原theme_entry_meta()函数
修改函数,强制绑定Post上下文,确保内部能获取到有效Post ID:
if ( ! function_exists( 'theme_entry_meta' ) ) : function theme_entry_meta( $theme_show_author = true, $theme_show_date = true, $theme_show_categories = true, $theme_show_tags = true, $theme_show_comments = true, $theme_human_diff = null, $theme_title_section = false, $post_id = null ) { // 确定有效Post ID:优先用传入值,否则取当前Post ID $post_id = $post_id ?: get_the_ID(); if ( !$post_id ) return; // 无有效ID直接返回 // 临时切换并绑定Post上下文 global $post; $original_post = $post; $post = get_post( $post_id ); setup_postdata( $post ); // 以下保留原函数的所有代码 do_action( 'theme_entry_meta_before', $theme_show_author, $theme_show_date, $theme_show_categories, $theme_show_tags, $theme_show_comments, $theme_human_diff, $theme_title_section ); $show_comments_first = ( 'default-centered' !== theme_option( 'blog_layout' ) ) && ( ! is_singular() ); if ( ! empty( $theme_show_comments ) && ! empty( $show_comments_first ) ) : theme_comment_count( $theme_title_section ); endif; if ( ! empty( $theme_show_author ) ) : do_action( 'theme_entry_meta_before_author', $theme_title_section ); theme_the_author( $theme_title_section ); endif; if ( ! empty( $theme_show_date ) ) : do_action( 'theme_entry_meta_before_date', $theme_title_section ); theme_the_date( $theme_human_diff, $theme_title_section ); endif; if ( ! empty( $theme_show_categories ) ) : do_action( 'theme_entry_meta_before_categories', $theme_title_section ); theme_the_categories( $theme_title_section ); endif; if ( ! empty( $theme_show_tags ) ) : do_action( 'theme_entry_meta_before_tags', $theme_title_section ); theme_the_tags( $theme_title_section ); do_action( 'theme_entry_meta_after_tags', $theme_title_section ); endif; if ( ! empty( $theme_show_comments ) && empty( $show_comments_first ) ) : theme_comment_count( $theme_title_section ); endif; do_action( 'theme_entry_meta_after', $theme_show_author, $theme_show_date, $theme_show_categories, $theme_show_tags, $theme_show_comments, $theme_human_diff, $theme_title_section ); // 恢复原始Post上下文,避免影响后续代码 wp_reset_postdata(); $post = $original_post; } endif;
调用时如果仍有问题,可手动传入Post ID:
theme_entry_meta(true, true, true, true, true, null, false, get_the_ID());
方案2:修复自定义函数的参数与样式问题
你的自定义函数存在语法错误(true | false是无效写法)和逻辑错误(直接赋值而非条件判断参数),修正后复用原主题CSS类即可:
function custom_entry_meta( $show_author = true, $show_category = true, $show_date = true, $show_tags = true, $show_comments = true ){ $post_id = get_the_ID(); if(!$post_id) return; // 复用原主题的元数据容器类,确保样式一致 echo '<div class="entry-meta">'; if($show_author){ $author_id = get_post_field('post_author', $post_id); echo '<span class="author-meta">' . get_avatar($author_id, 63) . '<a href="' . get_author_posts_url($author_id) . '">' . get_the_author_meta('display_name', $author_id) . '</a></span>'; } if($show_category){ echo '<span class="category-meta">' . get_the_category_list(', ', '', $post_id) . '</span>'; } if($show_date){ echo '<span class="date-meta">' . get_the_date('', $post_id) . '</span>'; } if($show_tags){ echo '<span class="tag-meta">' . get_the_tag_list('', ', ', '', $post_id) . '</span>'; } if($show_comments){ echo '<span class="comments-meta"><a href="' . get_comments_link($post_id) . '">' . get_comments_number($post_id) . ' 条评论</a></span>'; } echo '</div>'; }
前置检查
确保你的single_*.php中,函数调用是在主循环内部(即while ( have_posts() ) : the_post();和endwhile;之间),否则无法获取当前Post数据。
内容的提问来源于stack exchange,提问作者xii
相关产品推荐
相关产品推荐

