如果字符串中不存在作者元数据,则使用CSS显示部分



我在Wordpress上使用Elementor Pro构建了一个作者页面,并在页面的不同部分中显示各种作者元数据。如果一个节不包含作者元数据,我想向作者显示一条消息。

也就是说,如果citystyle_of_playhighest_division都不存在,则显示profile_info_template(默认设置为display: none)

当我只使用city时,我可以让它工作,但当我添加其他2块元数据时,它停止工作。这方面的任何指导都是much感激。

function nothing_to_show_display(){

global $post;
$author_id=$post->post_author;
$profile_info = get_the_author_meta('city', 'style_of_play', 'highest_division', 
$author_id);

if(empty($profile_info)) : ?>
<style type="text/css">
#profile_info_template   {
display: inline-block !important;
}
</style>;
<?php endif;

}

add_action( 'wp_head', 'nothing_to_show_display', 10, 1 );

它停止工作的原因是因为使用该函数一次只能请求一个数据值。https://developer.wordpress.org/reference/functions/get_the_author_meta/div -评论- 3500

我的建议是修改代码,一次只调用一个值,然后使用"OR"操作符在if语句中,像这样:

$author_city = get_the_author_meta('city', $author_id);
$author_style_of_play = get_the_author_meta('style_of_play', $author_id);
$author_highest_division = get_the_author_meta('highest_division', $author_id);
if(empty($author_city) || empty($author_style_of_play) || empty($author_highest_division)) : ?>
<style type="text/css">
#profile_info_template   {
display: inline-block !important;
}
</style>;
<?php endif;

如果您不打算使用这些值,那么简化代码并将函数放在if语句中是完全可以的。

if(empty(get_the_author_meta('city', $author_id)) || empty(get_the_author_meta('style_of_play', $author_id)) || empty(get_the_author_meta('highest_division', $author_id))) : ?>
<style type="text/css">
#profile_info_template   {
display: inline-block !important;
}
</style>;
<?php endif;

最新更新