Skip Navigation

[Resolved] How to display a post title with a maximun length character limit?

This support ticket is created 5 years, 10 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.

Our next available supporter will start replying to tickets in about 1.66 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by francescoG-3 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1191540

I am trying to: I have created a posts list and I'm trying to display the Related Post Title but with a max length.
I have tried to insert it into the View but it don't work.

[wpv-post-link id='[wpv-post-id]' length="16" more="..."]

Link to a page where the issue can be seen: hidden link

I expected to see: the location name limited ad 16 characters plus the more "..."
Ex: The "San Valentino In Abruzzo Citeriore" I need to be "San Valentino In ..."

Instead, I got: The normal length.

#1191541

So,
in custom code section, I have created this shortcode

add_shortcode( 'title', 'title_trim_words' );

function title_trim_words( $title )
{
      // limit to ten words
    return wp_trim_words( $title, 16, '...' );
}

And I have added it, [title] into the View, but it don't return nothing...

#1191695

I have created a posts list and I'm trying to display the Related Post Title but with a max length.
Hi, the wpv-post-link shortcode is used to display a link, not just the post title. If you just want to display the title, you can use the wpv-post-title shortcode. However, neither wpv-post-link nor wpv-post-title supports the "length" attribute. If you want to know which attributes are supported by our shortcodes, you can refer to the documentation here:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-title
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-link

If you want to customize the length of the title, you need a custom shortcode. If you want to display that trimmed title in a link, you need more custom code that creates a link tag using the wpv-post-url shortcode, and inserts the trimmed title shortcode in the link text.

And I have added it, [title] into the View, but it don't return nothing...
Please add a die() statement to your code to confirm it is being executed:

add_shortcode( 'title', 'title_trim_words' );
 
function title_trim_words( $title )
{
die( 'your code is executed with ' . $title . ' as the title');
      // limit to ten words
    return wp_trim_words( $title, 16, '...' );
}

If you do not see the words "your code is executed with...as the title" appear on the page, then check to be sure your snippet is Activated. My guess is that your $title variable is empty, because you don't have any shortcode attributes. Please consult the Shortcode API documentation in the WordPress Codex: https://codex.wordpress.org/Shortcode_API
Check the section "Enclosing vs self-closing shortcodes" to see examples accessing shortcode attributes and wrapped contents.

#1192715

My issue is resolved now. Thank you!

I hava found this solution that work fine:

add_shortcode( 'title', 'title_trim_words' );
  
function title_trim_words( $title )
{
  $title= substr(get_the_title(), 0, 17 );
  $length=strlen(get_the_title());
  if ($length > 17) {$more="…";}
      // limit to ten words
    return $title . $more ;
}