Skip Navigation

[Closed] How to check in a View for length of CF to conditionally output something?

This support ticket is created 3 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 3 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1901073

HI,

In a content template I need to output different CSS depending on the length of a certain custom field (hbh-address-city).

I thought to define both version of the CSS and to use a conditional statement in order to determine which of the two definitions I have to use.

Now I struggle how to integrate a function (also where exactly) that returns the length of a field and how this is used this in the content template.

I found a view links, tried them but nothing worked so far.

#1901249

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand, you have a custom field, and for that custom field you want to calculate the number of characters and then based on the length returned - you want to apply condition.

If this is true, to calculate the langth of the custom field content you will require to write customs shortcode.

For example:

add_shortcode('field_length', 'func_calculate_field_length');
 function func_limit_field_length($atts) {
global $post;
    $field =  "wpcf-".$atts['field'];
  $field_value =   get_post_meta($post->ID,$field, true);
  $field_length = strlen($field_value);
 
  if($field_length < 50){
       return 1;
 }else{
      return 0;
}
     
}

And you can call the above shortcode as:

[field_length field="my-field-slug"]

And later, you can use this shortcode with [wpv-conditional] statement or if you are using blocks then with conditional block.

Please let me know if this is clear or you will require further help.

more info:
=> https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/
=> https://toolset.com/block-item/conditional/

#1901371

Hi Minesh,

Thanks for your answer. I still do not get any results. See what I did:

1. implemented your suggestion as a plugin and activated it:

add_shortcode('tsmp_field_length', 'func_calculate_field_length');

function func_limit_field_length($atts) {
	global $post;
    $field =  "wpcf-".$atts['field'];
	$field_value =   get_post_meta($post->ID,$field, true);
	$field_length = strlen($field_value);
	
	if($field_length > 15){
		return 1;
	}else{
		return 0;
	}  
}

2. In a Content-Template I added:

Test: [tsmp_field_length field="hbh-adresse-stadt"]
Real: [wpv-conditional if="( [tsmp_field_length field="hbh-adresse-stadt"] eq '1' )"]zu groß[/wpv-conditional]

What I get is:

Test: [tsmp_field_length field="hbh-adresse-stadt"] Real:

So, the shortcode does not display anything directly and also does not show anything in the conditional context (where a custom field „ hbh-adresse-stadt “ contains „Immenstadt i. Allg.“

DO I habe t register the shortcode also in the Settings -> Frontend-Content ?? I tried anyway already, but nothing changed in the behaviour ouf the output.

Any ideas?

#1901917

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes, you need to register the shortcode at Settings -> Frontend-Content.

That is strange. Can you please share problem URL and access details so I can what whats going wrong with your setup.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

The topic ‘[Closed] How to check in a View for length of CF to conditionally output something?’ is closed to new replies.