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

Display section using CSS if no author metadata exists in a string

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

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

当我只使用 city 时我可以让它工作,但是当我添加其他 2 个元数据时它停止工作。对此的任何指导都将 非常 表示赞赏。

    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-comment-3500

我的建议是将您的代码修改为一次仅调用一个值,然后在您的 if 语句中使用“OR”运算符,如下所示:

    $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;