<?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
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…
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
}