Skip Navigation

[Resolved] Trying to get character count of title

This thread is resolved. Here is a description of the problem and solution.

Problem:

Check the post title length with custom shortcode.

Solution:

It needs custom codes, see details here:

https://toolset.com/forums/topic/trying-to-get-character-count-of-title/#post-1284435

Relevant Documentation:

https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes

This support ticket is created 5 years, 5 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
- 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/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by JamesK4399 5 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#1284415

I am trying to make the project title accommodate titles of up to 4 lines with 4 different div tags. I think I'm really close but not sure what I'm doing wrong.

I went to settings->Front-End Content and added [get-char-count].

I added custom code for calc-length:

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
function func_get_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( 'get_char_count', 'func_get_char_count' );

Finally in the layout I have this code:

[wpv-conditional if="( '[get_char_count]' lte 22 )"]
<div class="white-project-name">[wpv-post-title]</div>[/wpv-conditional]
[wpv-conditional if="( '[get_char_count]' gte 23 ) AND ( '[get_char_count]' lte '51' )"]<div class="white-project-name-two-lines">[wpv-post-title]</div>[/wpv-conditional]
[wpv-conditional if="( '[get_char_count]' gte 52 ) AND ( '[get_char_count]' gte '76' )"]<div class="white-project-name-three-lines">[wpv-post-title]</div>[/wpv-conditional]
[wpv-conditional if="( '[get_char_count]' gte 77 )"]
<div class="white-project-name-four-lines">[wpv-post-title]</div>[/wpv-conditional]

which always choose the 1 option.

Should I pass the post id to my custom code (the $atts parameter)?

#1284435

Hello,

I suggest you try these:
1) Modify your custom PHP codes as below:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

function func_get_char_count( $atts ) {
global $post;

if(!isset($atts['type'])) {
return strlen($post->post_title);
}else if($atts['type']=="post_type"){
return strlen($post->post_type);
}
return FALSE;
}
add_shortcode( 'get_char_count', 'func_get_char_count' );

2)Since you are using custom shortcode [get_char_count] within [wpv-conditional] shortcode, please check these:
Dashboard-> Toolset-> Settings-> Front-end content
in section "Third-party shortcode arguments", add the your custom shortcode name: get_char_count

And test again.

More help:
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes

#1284505

My issue is resolved now. Thank you!