Skip Navigation

[Resolved] Question about retrieving metadata about a featured image

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

Problem: I would like to write a conditional that tests whether or not a post's featured image has a caption assigned in the Media Library. Can I use the wpv-post-featured-image shortcode in a conditional?

Solution:
Yes, you can use the wpv-post-featured-image shortcode in a conditional:

[wpv-conditional if="( '[wpv-post-featured-image output="caption"]' ne '' )"]
This featured image has a caption.
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-post-featured-image

This support ticket is created 5 years 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)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by josephC-5 5 years ago.

Assisted by: Christian Cox.

Author
Posts
#1401701

Hi there,

I'm trying to retrieve metadata from a featured image. What I want to do is check if a featured image has a non-empty Caption field. Obviously I need a conditional, but I don't know what goes inside it:

[wpv-conditional if="( 'something_here' ne '' )"]
...
[/wpv-conditional]

According to the documentation (https://toolset.com/documentation/user-guides/views-shortcodes/#vf-153425), I can use the shortcode [wpv-post-featured-image].

Is it as simple as the code below?

[wpv-post-featured-image output="caption"]

Thanks!

Saul

#1402291

Hi, yes it may be just that simple depending on where you intend to place this conditional. For example, if you want to test the featured image of the current post, the code would look like this:

[wpv-conditional if="( '[wpv-post-featured-image output="caption"]' ne '' )"]
This featured image has a caption.
[/wpv-conditional]

However, if you want to test the caption of some other post arbitrarily, you need to specify the post ID using the "item" attribute:

[wpv-conditional if="( '[wpv-post-featured-image output="caption" item="12345"]' ne '' )"]
The featured image for post ID 12345 has a caption.
[/wpv-conditional]

Let me know if you have questions about that.

#1404845

That's super easy. Thanks, Christian!

Saul