Skip Navigation

[Resolved] Conditional formatting for a field that has an ID passed in it

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

Problem: I have a custom field on User profiles. I would like to write a conditional that tests whether or not any content exists in the field for the current author archive User.

Solution:
Add this custom code:

function get_author_twitter_in_archive($atts) {
  $author_id = get_the_author_meta('ID');
  return types_render_usermeta( 'twitter', array( 'user_id' => $author_id ) );
}

Then in the WordPress Archive:

[wpv-conditional if="( get_author_twitter_in_archive() ne '' )"]
this author has twitter information available
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

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

This topic contains 7 replies, has 2 voices.

Last updated by matthewL-7 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1205643

Hi,

I am using the following custom field:
[wpv-user field="wpcf-twitter" id="[get_user_id]"]

This gets the information from the custom field wpcf-twitter for the User ID the page is about.

However I want to setup custom output so that if this field is blank then content does not show, I would normally know how to do this using the platform but it doesn't let you setup with the ID, I found the code above from a previous Toolset thread.

How can I setup a conditional output based on the above?

Thanks!

#1205782

You would have to write a custom function to do this. Otherwise, the required "if" statement conditions would exceed the maximum number of wpv-conditional shortcode nested attributes. Here's an example:

function get_author_twitter_in_archive($atts) {
  $author_id = get_the_author_meta('ID');
  return types_render_usermeta( 'twitter', array( 'user_id' => $author_id ) );
}

Register get_author_twitter_in_archive, then you can use this function directly in your conditional.

#1206347

Hey Christian

Thanks for this but for some reason this didn't work.

It doesn't look like the custom shortcode is working.

I used your snippet and I added the shortcode in Settings -> Front end content and then added it under "Third-party shortcode arguments"

Tried just using [get_author_twitter_in_archive] on the page but it didn't get replaced just prints the actual shortcode on the page.

Thanks.

#1206424

Tried just using [get_author_twitter_in_archive] on the page but it didn't get replaced just prints the actual shortcode on the page.
Right the code I gave you doesn't register a shortcode, so it won't work like that. The code was simply a custom function you can use in a conditional to test the current archive User's "twitter" field:

[wpv-conditional if="( get_author_twitter_in_archive() ne '' )"]
this author has twitter information available
[/wpv-conditional]

I used your snippet and I added the shortcode in Settings -> Front end content and then added it under "Third-party shortcode arguments"
It should be registered in "Functions inside conditional evaluations" since it's not really a shortcode.

If you want to register a shortcode so you can output the twitter information, you would add a line like this:

add_shortcode("get_author_twitter_in_archive", "get_author_twitter_in_archive");

Then the shortcode you mentioned would be available.

#1207067

Hi Christian,

Ah OK I see, so I swapped how I have registered the short code to what you have suggested but still doesn't seem to be working.

I setup the following code for example:

[wpv-conditional if="( get_author_twitter_in_archive() ne '' )"]test[/wpv-conditional]

But test prints every time even if the Twitter field is filled in or not.

Snippet I am using

function get_author_twitter_in_archive($atts) {
  $author_id = get_the_author_meta('ID');
  return types_render_usermeta( 'twitter', array( 'user_id' => $author_id ) );
}
#1207187

May I login to see this in wp-admin? The shortcode works fine in my local environment so there must be something else going on.

#1209537
trixie.png
reece.png

I'm not following, sorry. This user has no information for the wpcf-twitter custom field in their profile, and I don't see the "test" text printed in the author archive:
hidden link
See trixie.png

On the other hand, this user has information in their Twitter custom field, so I see the "test" text printed:
hidden link
See reece.png

As far as I understand it, the conditional is working as expected. The text "test" shows up for Reece because he has Twitter custom field information. It does not show up for Trixie because she has no Twitter custom field information. Can you clarify for me?

#1209962

Yeah it is working now... I have no idea who fixed it. Either way thanks for your help on this matter! 🙂

It is fully working now. Cheers.