Skip Navigation

[Resolved] Display a default image when user feature image is empty

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display an avatar image from a custom field, if that field has been set for the current post. If not, I would like to display a default image instead.

Solution: Use conditional statements that check the value of the avatar custom field. If the field is empty, show an image from your Media Library by using the Add Media button. If the field is not empty, show the custom field image using the Fields and Views button. Example:

[wpv-conditional if="( $(wpcf-avatar) ne '' )"]
[types field='avatar' alt='%%ALT%%' title='%%TITLE%%' size='full' align='none'][/types]
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-avatar) eq '' )"]
<img src="http://yoursite.com/wp-content/uploads/2017/08/765-default-avatar-150x150.png" width="150" height="150" class="size-thumbnail" />
[/wpv-conditional]

Relevant Documentation: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/customizing-sites-using-php/functions/#image

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#556275

Now I am using Cred user post to build my site, I saw a good user field to display Avatar, that is user feature image, it allows user to upload a thumbnail as an Avatar, it is very good, I think I can remove my custom field to display user photos.

but it is not a custom field, that means I can not set up a default image. I just wanna show a default a image on the cred post when user submitted the form with no image uploaded, otherwise, position the of avatar is very ugly. I know I can use conditional output as when feature image is empty, then display a image URL, how I should do? I guess that needs some short code to display a image by url. I am a WP beginner, I do not know coding at all. could you please show me how to do that? 🙂 thank you so much

#556290
step-4.png
step-1.png

Sure, you can do this with any image in your Media Library used as the default image, and a conditional statement.

1. Use the "conditional output" button to insert a conditional statement. Choose your custom avatar field, and test condition "!=", then insert the conditional. See step-1.png.

2. Place your cursor after the [wpv-conditional] tag. Then use the "Fields and Views" button to insert your custom avatar field inside the conditional tag. Click the "/conditional output" button again to close the conditonal. You should now have code that looks like this:

[wpv-conditional if="( $(wpcf-avatar) ne '' )"]
[types field='avatar' alt='%%ALT%%' title='%%TITLE%%' size='full' align='none'][/types]
[/wpv-conditional]

The field names may be different based on your custom field name. Now you have a conditional that will show the avatar custom field if the user has uploaded one.

3. Place your cursor after the [/wpv-conditional] tag. Repeat step 1, but this time test the avatar custom field with condition "=". Insert the conditional.

4. Place your cursor after the [wpv-conditional] tag you just created. Use the "Add Media" button to insert an image from your Media Library. Then click "/conditional output" to close the conditional. You will end up with some code that looks like this:

[wpv-conditional if="( $(wpcf-avatar) eq '' )"]
<img src="<em><u>hidden link</u></em>" width="150" height="150" class="size-thumbnail" />
[/wpv-conditional]

See step-4.png. Now if the custom field is empty, your default image will appear. Please let me know if this will not work for you and I will take another look.

#557895

Thank you for your detailed answer, I made it! have a great day! :)