Skip Navigation

[Resolved] Show content only if a custom field is not empty

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

Problem:
Client wants to display a section only if a custom field that appears in that section is not empty. If there is no value the whole section should not be displayed.

Solution:
Use the wpv-conditional shortcode.

An example using a custom field with slug "sermons-audio" would be:

[wpv-conditional if="( NOT(empty($(wpcf-sermons-audio))) )"]
<h4>Listen to the sermon</h4>
<p>Some other conditional stuff</p>
<p>Then the field itself</p>
[types field="sermons-audio"][/types]
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/

100% of people find this useful.

This support ticket is created 6 years, 7 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.

Our next available supporter will start replying to tickets in about 8.71 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 8 replies, has 2 voices.

Last updated by AtefR7377 6 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#656534
Capture.JPG

Hi,
I have a custom field with sluy "sermons-audio".
it is an audio field, so it has the url of an audio file.
like this:

[audio src="audio url" /]

it is working perfectly by itself.

I want to display a title above it saying "Listen to the sermon", in addition to some other stuff. However, not all my posts will have this audio files. so i don't want to put the title without having the sermon in the post

Therefore, I want to create a shortcode to display the html I want, along with the shortcode content, but only if the custom field is not empty.

I wrote this test shortcode, it shows the audio player, but it shows it above all other content in the Layout. and it displays the player Twice.

add_shortcode( 'sermon-audio-link', 'sermon_audio_link' );
function sermon_audio_link () {
  if( function_exists( 'types_render_field' ) ){
if ( !empty( types_render_field( 'wpcf-sermons-audio', array() ) ) ) {
echo(types_render_field( 'sermons-audio', array() ));
}
}
}

I also modified it and removed the

types_render_field

, like this, but nothing happened too:

add_shortcode( 'sermon-audio-link', 'sermon_audio_link' );
function sermon_audio_link () {
  if( function_exists( 'types_render_field' ) ){
if ( !empty( 'sermons-audio' ) ) {
echo(types_render_field( 'sermons-audio', array() ));
}
}
}

So can you please help me?

i checked the php documentation for the plugin, but it has a way to show the field only.

thanks a lot

#657280

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Atef

You can conditionally show content in a Toolset template without having to touch the theme template files or write any PHP using the wpv-conditional shortcode.

That is described generally here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

And specifically checking for an empty field is described here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/

So if you had a Types audio field with the slug of 'sermons-audio' you should be able to add the following to your Content Template or Visual Editor cell of a Layout:

[wpv-conditional if="( NOT(empty($(wpcf-sermons-audio))) )"]
<h4>Listen to the sermon</h4>
<p>Some other conditional stuff</p>
<p>Then the field itself</p>
[types field="sermons-audio"][/types]
[/wpv-conditional]
#657347

Hi Nigel,

Thank you very very much for such prompt response and excellent solution.

I tried it an worked perfectly.

Only one tweak needed for me please,

The view I am working on now is a view that shows two custom post types, sermons and books.
I want to show an element (the featured image) only when the post type is book.

so i added this part to the code, but it did not show anything:

[wpv-conditional if="( is_singular( 'book' ))"]
[wpv-post-featured-image size="medium"]
[/wpv-conditional]

so is there a better way to target a certain post type in the loop? i could not find such conditional in the GUI.

thanks in advance.

#657449

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

If you want to use custom functions (including WordPress functions) or shortcodes in the conditions you need to register them so that Views recognises them.

Go to Toolset > Settings > Front-end Content and register the function.

This is described here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

Note the following: "If you expect the function to return a boolean, the value to check agains should be 1 or ‘1’ for true and 0 or ‘0’ for false."

#657969

Hi Nigel,

Thanks again for your help.

I went to settings, and added

is_singular

. then I edited the loop wizard, and saw the new function,
then I was able to put the following conditional:

[wpv-conditional if="( is_singular() eq 'book' )"]
[wpv-post-featured-image size="medium"]
[/wpv-conditional]

However, hothing showed up.

so am I doing it wrong? I checked the documentation ,but there are no comparable examples that I saw.

the other option is: are there any built in functions that can target a specific custom post type in the conditional?
maybe

is_singular

is not the best solution?

this is what i am referring to, it even has a "book" example in it.
https://developer.wordpress.org/reference/functions/is_singular/
thanks again.

#672274

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

is_singular needs the post type passing as an argument, and will return true or false, which with the wpv-conditional shortcode should be tested as either 1 or 0.

So I think you want something more like this:

[wpv-conditional if="( is_singular( 'book') eq 1 )"]
[wpv-post-featured-image size="medium"]
[/wpv-conditional]

I haven't tested it but that should work. If not try enclosing 1 in single quotes -> '1'

#673464
07.JPG

Hi Nigel,
thanks for the reply.

I added the code with and without the quotes around the 1. both did not work.

here is the full code I have:

[wpv-post-link]</br>
[wpv-conditional if="( is_singular( 'book' ) eq '1' )"]
[wpv-post-featured-image size="medium"]
[/wpv-conditional]
[types field='sermons-video' height='300'][/types]

Here is the page with the view:
hidden link

you will see that the first 2 post (refer attached screenshot) with post titles "1" and "testzz" both have featured images, yet they are not showing in the view.

All what I am trying to achieve is a simpoe view that shows featured image when it is a book CPT and shows a certain custom field when it is a video CPT.

thanks

#673969

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry, just re-reading your question I understand the problem.

You are using this in a View.

In which case is_singular won't work, because that tests whether you are on a single post page of a particular type, which with your set up you will not be.

The post type of the current post in the Loop is available with the wpv-post-type shortcode, which you can use in the conditional test, something like this:

[wpv-conditional if="( '[wpv-post-type]' eq 'book' )"]
	<p>This is a single book post</p>
[/wpv-conditional]

[wpv-conditional if="( '[wpv-post-type]' eq 'video' )"]
	<p>This is a single video post</p>
[/wpv-conditional]

That should now work.

#674118

Nigel, You are a superstar...Finally the problem is solved.

I really can't thank you enough....

WP Types support team gives the best support I have received.

Thanks a lot.