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!
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.
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.
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.
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 ) );
}
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.
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?
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.