WordPress.org

Support

Support » Plugins and Hacks » Author Image » [Resolved] How to check if Author image is blank

[Resolved] How to check if Author image is blank

  • Hi,

    I want to check if the user does not upload an image. How do I do that with this plugin?

    I tried this and it does not work:

    <?php $author = get_userdata( get_query_var('author') );?>
    
    <?php if ( !empty($author->the_author_image) ) { ?>
    
    <?php the_author_image();?>
    
    <?php } ?>

    but it does not work.

    Any suggestions would help!

    Thanks,

    JC

    https://wordpress.org/plugins/sem-author-image/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Mike Koepke

    @mike_koepke

    <?php $author = get_userdata( get_query_var(‘author’) );?>

    <?php $author_image = get_user_meta($author, ‘author_image’, true); ?>

    <?php
    if ( $author_image === ”” )
    echo “No image uploaded”;
    else
    echo “Image is ” . $author_image;

    ?>

    Hi Mike,

    At present, if an author has no image assigned, the_author_image() returns their name instead.

    So trying to implement your code above, but $author_image is returning empty in all cases, even for those authors with an image.

    Is there an other way to check if an image is present?

    Cheers,

    Andy

    Plugin Author Mike Koepke

    @mike_koepke

    Let me test this. It is possible that it can’t detect the actual author so it returns empty.

    Are you using the code in a sidebar or a post?

    Hi Mike, I’m using it in a author page template within the post loop…

    Plugin Author Mike Koepke

    @mike_koepke

    Andy,

    There actually is a helper function in the plugin called get_author_id. It attempts to resolve the author in a few different ways.

    You can do:

    <?php $author = $author_image->get_author_id(); ?>

    This should give you a valid author id to query for the author_image meta data.

    The code for the function is:

    function get_author_id() {
    
    		$author_id = null;
    
    		if ( in_the_loop() ) {
    			$author_id = get_the_author_meta('ID');
    		} elseif ( is_singular() ) {
    			global $wp_the_query;
    			$author_id = $wp_the_query->posts[0]->post_author;
    		} elseif ( is_author() ) {
    			global $wp_the_query;
    			$author_id = $wp_the_query->get_queried_object_id();
    		}
    
    		return $author_id;
    	} #get_author_id()

    I have the same issue. I try this code but it won’t work.

    if ( the_author_image ( $user->ID ) ) {
        the_author_image( $user->ID );
    }
    else {
        // echo another picture instead
    }

    Basically, if a user has uploaded an author image, it should be displayed. If not, it displays another picture.

    I just figured it out, here is my solution;

    $author_image = get_user_meta( $user->ID , 'author_image', true );
    if ( $author_image ) {
        the_author_image();
    } else {
        // echo another picture
    }

    Nice one – thanks Mike 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] How to check if Author image is blank’ is closed to new replies.
Skip to toolbar