Sauter la navigation

[Résolu] Calculating the Length of Post Titles and of Custom Fields

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Calculating the Length of Post Titles and of Custom Fields

Solution:
You need to write custom shortcode to calculate the number of characters of post title or custom field value.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/calculating-the-length-of-post-titles-and-of-custom-fields/#post-1211701

Relevant Documentation:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

This support ticket is created Il y a 5 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.

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)

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

Dernière mise à jour par alexG-4 Il y a 5 années et 9 mois.

Assisté par: Minesh.

Auteur
Publications
#1211258

I want to display the lengths of some Post Titles and of Custom Fields.

I've found some threads about calculating the length of a custom field within a conditional, but I'm afraid I wasn't able to convert that answer into a simple display of the lengths.

Thanks

Alex

#1211289

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - do you mean that you want to display the total number of character value? Where exactly you want to display the total character count value? in admin or frontend?

#1211332

I want to display the length of the Title of a post, and the length of a Custom Post Type - both in the front-end.

#1211334

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

when do you say length in what format you want to calculate it - in characters or words?

#1211358

In characters

#1211359

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Well - can you please try to add the following shortcode to your current theme's functions.php file.
OR
You can then add the custom shortcode to Toolset's custom code section:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

function func_show_char_count( $atts ) {
 global $post;

If($atts['type']=="title") {
      return strlen($post->post_title);
}else if($atts['type']=="post_type"){
   return strlen($post->post_type);
}
   return FALSE;
}
add_shortcode( 'show_char_count', 'func_show_char_count' );

You can call the shortcode as given under:
To display Title character count:

[show_char_count type="title"] 

To display Post Type character count:

[show_char_count type="post_type"] 
#1211483

Thanks, Minesh

The "title" version works perfectly, but I'm confused about the "post_type" version and I'm not familiar enough with PHP - especially in a WP environment - to be able to decode what you've done.

I'll want to use the short-code with various custom fields - for example 'basic-topic'.

With that, I've tried

[show_char_count type="basic-topic"]

and

[show_char_count type="wpcf-basic-topic"]

But neither works.

BTW - this is all within a View loop - in case that makes a difference.

#1211701

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Well - Ive changed the shortcode so that it supports the custom field.

function func_show_char_count( $atts ) {
 global $post;
 
If($atts['type']=="title") {
      return strlen($post->post_title);
}else if($atts['type']=="post_type"){
   return strlen($post->post_type);
}else if($atts['type']=="field"){
  $field = $atts['name'];
   $value = get_post_meta($post->ID,"wpcf-".$field,true);
   return strlen($value);
}

   return FALSE;
}
add_shortcode( 'show_char_count', 'func_show_char_count' );

And you can call it as given under:

[show_char_count type="postfield" name="basic-topic"]
#1211847

Hi Minesh

Thanks! That works. And I see now that my original question was not clear enough about what I needed, so thanks for your patience.

I think this is a useful facility, so if it get written up as a "Solution", please note that the usage should include type="field", rather than type="postfield".

Alex

#1211848

My issue is resolved now. Thank you!