Tell us what you are trying to do?
Add social media icons as links based values in Toolset Custom User Fields.
Is there any documentation that you are following?
https://toolset.com/forums/topic/display-first-and-last-name-in-stead-of-nickname-in-cpt-relationship-with-user/
I've followed the example at the above link but so far unable to get this to work. See my screenshot for details.
Perhaps this conditional needs to be tweaked to follow the custom shortcake format used for the author id, specified in this other post of mine? https://toolset.com/forums/topic/user-custom-field-display/
What is the link to your site?
hidden link
Hi,
Thank you for contacting us and I'd be happy to assist.
From the screenshot, I understand that you're showing these user custom field values in the author archive, and to get the current archive's author ID, you have a custom shortcode "get-author-id-in-archive".
To check for the existing user field values, you can register another custom shortcode, that can return 1 if the value of a user field exists and 0 if it doesn't:
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/ )
function get_author_in_archive_value_check_func($atts) {
$a = shortcode_atts( array(
'field' => ''
), $atts );
$author_id = 0;
if (is_author()){
$author = get_queried_object();
$author_id = $author->ID;
$field_value = types_render_usermeta( $a['field'], array( "user_id" => $author_id ) );
if(!empty($field_value)) {
return '1';
}
else {
return '0';
}
}
}
add_shortcode("get_author_in_archive_value_check", "get_author_in_archive_value_check_func");
Next, please add "get_author_in_archive_value_check" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.
After that you'll be able to use this new shortcode, in a conditional shortcode like this:
[wpv-conditional if="( '[get_author_in_archive_value_check field='user-facebook']' ne '' )"]
<a href="[types usermeta='user-facebook' output='raw' user_id='[get-author-id-in-archive]'][/types]">Link</a>
[/wpv-conditional]
In this example, I've used a conditional check for the URL type user field "user-facebook" and you can adjust it as needed for your own fields.
Note: The WordPress "Shortcode" block is designed for simple shortcodes and when you have to use complex nested shortcodes like conditional shortcodes, it is better to use Toolset's "Fields and Text" block.
regards,
Waqar
Thank you Waqar. This is getting close but it seems to be showing the link regardless of whether the field is empty.
Here is the code you provided tweaked for displaying a social icon.
[wpv-conditional if="( '[get_author_in_archive_value_check field="aceer-profile-facebook-link"]' ne '' )"]
<a href="[types usermeta='aceer-profile-facebook-link' output='raw' user_id='[archive-author-id]'][/types]"><img src="/wp-content/uploads/2021/03/facebookicon-1320193917989505812_0.svg" style="width:40px;height:40px;">Link</a>
[/wpv-conditional]
This is currently showing the icon and linking out to the value specified in the field if there is a value, but the icon is also showing if the field is empty. See this page which works as described and should be showing the icon and link:
hidden link
vs, this page, which has an empty acer-profile-facebook-link field and should not be showing the icon at all:
hidden link
Thanks for writing back and it is strange that the icon is still showing.
Can you please share temporary temporary admin login details, in reply to this message?
This will help in troubleshooting the conditional check and the custom shortcode.
Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.
Thank you for sharing the admin access and I apologize for an oversight on my part.
The custom shortcode that I shared earlier returns 1 if the field value is not empty and 0 if it is empty. This is why the conditional code should look for value 1 ( eq '1' ) and not compare it with the empty value ( ne '' ) as the returned value is never empty.
I've updated the conditional code for the icons to check for the value 1 and it is working now:
[wpv-conditional if="( '[get_author_in_archive_value_check field='aceer-profile-facebook-link']' eq '1' )"]
<a href="[types usermeta='aceer-profile-facebook-link' output='raw' user_id='[archive-author-id]'][/types]"><img src="/wp-content/uploads/2021/03/facebookicon-1320193917989505812_0.svg" style="width:40px;height:40px;"></a>
[/wpv-conditional]
[wpv-conditional if="( '[get_author_in_archive_value_check field='aceer-profile-instagram-link']' eq '1' )"]
<a href="[types usermeta='aceer-profile-instagram-link' output='raw' user_id='[archive-author-id]'][/types]"><img src="/wp-content/uploads/2021/03/iconfinder_2018_social_media_popular_app_logo_instagram_3225191.svg" style="width:40px;height:40px;"></a>
[/wpv-conditional]
[wpv-conditional if="( '[get_author_in_archive_value_check field='aceer-profile-youtube-link']' eq '1' )"]
<a href="[types usermeta='aceer-profile-youtube-link' output='raw' user_id='[archive-author-id]'][/types]"><img src="/wp-content/uploads/2021/03/iconfinder_youtube_834723.svg" style="width:40px;height:40px;"></a>
[/wpv-conditional]
[wpv-conditional if="( '[get_author_in_archive_value_check field='aceer-profile-personal-website-link']' eq '1' )"]
<a href="[types usermeta='aceer-profile-personal-website-link' output='raw' user_id='[archive-author-id]'][/types]"><img src="/wp-content/uploads/2021/03/iconfinder__web_Internet_network_www_communication_global_worldwide_4213462.svg" style="width:40px;height:40px;"></a>
[/wpv-conditional]