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

Time:10-24

I have built an author page using Elementor Pro on Wordpress, and display various pieces of author metadata on the page within different sections. I would like to display a message to the author if a section doesn't contain author metadata.

In other words, if none of city, style_of_play or highest_division exist, then display profile_info_template (which is set to display: none by default)

I can get this to work when I use city only, but it stops working when I add the other 2 pieces of metadata. Any guidance on this would be much appreciated.

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

CodePudding user response:

The reason it stops working is because with that function you can only request one value of data at a time. https://developer.wordpress.org/reference/functions/get_the_author_meta/#div-comment-3500

My suggestion is to modify your code to only call one value at a time, then use the "OR" operator within your if statement, like this:

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

Also if you don't plan to use those values it is perfectly fine to simplify the code and place the functions within the if statement.

    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;
  • Related