WordPress.org

Forums

Author Image
[resolved] How to check if Author image is blank (9 posts)

  1. joecannes
    Member
    Posted 1 year ago #

    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/

  2. Mike Koepke
    Member
    Plugin Author

    Posted 1 year ago #

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

    ?>

  3. AndyiBM
    Member
    Posted 1 year ago #

    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

  4. Mike Koepke
    Member
    Plugin Author

    Posted 10 months ago #

    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?

  5. AndyiBM
    Member
    Posted 10 months ago #

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

  6. Mike Koepke
    Member
    Plugin Author

    Posted 9 months ago #

    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()
  7. Filespit
    Member
    Posted 8 months ago #

    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.

  8. Filespit
    Member
    Posted 8 months ago #

    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
    }
  9. AndyiBM
    Member
    Posted 8 months ago #

    Nice one - thanks Mike :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Author Image
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.