WordPress Elementor作者页无指定元数据时通过CSS显示区块问询
问题原因
get_the_author_meta() 函数仅支持单次查询单个作者元字段,你传入3个元字段参数属于用法错误,因此无法正常判断三个字段的空值状态。
修复后代码
function nothing_to_show_display(){ global $post; // 仅在单内容页生效,避免非内容页出现PHP报错 if ( ! is_singular() || empty( $post->post_author ) ) { return; } $author_id = $post->post_author; // 分别查询三个元字段值 $city = get_the_author_meta( 'city', $author_id ); $style_of_play = get_the_author_meta( 'style_of_play', $author_id ); $highest_division = get_the_author_meta( 'highest_division', $author_id ); // 三个字段全部为空时输出覆盖样式 if( empty( $city ) && empty( $style_of_play ) && empty( $highest_division ) ) : ?> <style type="text/css"> #profile_info_template { display: inline-block !important; } </style> <?php endif; } add_action( 'wp_head', 'nothing_to_show_display', 10 );
补充说明
- 代码添加了单页判断逻辑,避免在首页、归档页等无有效
$post全局变量的场景下报错 - 用
&&逻辑与运算符实现「三个元数据全部不存在才显示提示块」的要求,如果你需要调整为「只要有一个元数据为空就显示提示块」,把&&替换为||即可 - 请确认你的元字段key拼写完全正确,大小写、下划线均需要和后台配置保持一致
内容的提问来源于stack exchange,提问作者Andrew
相关产品推荐
相关产品推荐

