Skip Navigation

[Resolved] Entering default image when featured image is empty in view templates

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.

This topic contains 5 replies, has 3 voices.

Last updated by houfton 6 years, 11 months ago.

Assigned support staff: Riccardo Strobbia.

Author
Posts
#90842

Hello. I have a view template which currently has this:

<a href="[wpv-post-url]"><IMG [wpv-post-featured-image]</IMG></a>

But there are several posts which don't have a featured image, so I need it to replace those ones with a default image instead. From reading previous posts and help on this area, I tried doing this:

<a href="[wpv-post-url]"><IMG [wpv-if [wpv-post-featured-image] evaluate="empty($image)"] "<em><u>hidden link</u></em>"[/wpv-if]</IMG></a>

But it didn't work... No images showed anywhere... so please can you tell me the correct syntax for a conditional clause to enter a default image when there is no featured image for a post, in a view template.

Thank you

#91197

Dear timB,

WordPress stores the featured image as a custom field called "_thumbnail_id".

You can test for this with code like this:

If the fetured image is present:

[wpv-if image="_thumbnail_id" evaluate="!empty($image)"]
    <div class="my_featured_image">
        [wpv-post-featured-image size="full"]
    </div>
[/wpv-if]

Otherwise:

[wpv-if image="_thumbnail_id" evaluate="empty($image)"]
    <div class="my_featured_image">
        <img src="LINK_TO_IMAGE" alt="" />
    </div>
[/wpv-if]

Please let me know if there is anything else that I can assist you with.

#91208

Thanks for that Riccardo. I'm still struggling with the logic though.

Is there an "if then else" syntax I can use for this. The first set of code inputs both the default image and the featured image (when there is one) and the second returns nothing.

So please let me clarify what's needed more precisely.

Lets call the path to the default image this: 'default_image_path'

So in plain English I need the code to do this:

If featured image exists, input featured image as thumbnail
Else: Input 'default_image_path' as thumbnail
end if

Could you translate that into code which Views can work with please?

Thank you
Tim

#91319

Dear timB,

I would explain further hope this will help.

The first portion of the code:

[wpv-if image="_thumbnail_id" evaluate="!empty($image)"]
    <div class="my_featured_image">
        [wpv-post-featured-image size="full"]
    </div>
[/wpv-if]

will display the featured image if it is present, none otherwise.

The second portion of the code will display a default image if no featured image is present:

[wpv-if image="_thumbnail_id" evaluate="empty($image)"]
    <div class="my_featured_image">
        <img src="default_image_path" alt="" />
    </div>
[/wpv-if]

The API doesn't have a "if then else statement", but same effect can be easily achieved with that, saying in plain english:

if you have one show it;
if you do not have one show default;

Please let me know if there is anything else that I can assist you with.

#91503

Excellent - thank you. It works.
T

#249655

This looks like the answer I am looking for but the code snippets seem to have disappeared...