Skip Navigation

[Résolu] Social Links Conditional in loop on Profile Page

This support ticket is created Il y a 3 années et 9 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par Waqar Il y a 3 années et 8 mois.

Assisté par: Waqar.

Auteur
Publications
#1973779

J S
Screen Shot 2021-03-03 at 1.34.07 PM.jpg

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?

lien caché

#1974233

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

#1976877

J S
Screen Shot 2021-03-06 at 1.36.22 PM.png

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:

lien caché

vs, this page, which has an empty acer-profile-facebook-link field and should not be showing the icon at all:

lien caché

#1978083

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.

#1980623

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]