Skip Navigation

[Résolu] Shortcode to limit post content has a problem with images

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

This topic contains 3 réponses, has 2 voix.

Last updated by Beda Il y a 2 années et 3 mois.

Assigned support staff: Beda.


I have been using a shortcode function to limit the displayed length of the post content within a view.

You can see it in operation at hidden link

Here is the function:-

// Use to limit the displayed number of characters in the event description
add_shortcode('limit_content', 'trim_shortcode');
 function trim_shortcode($atts, $content = '') {
    $content = wpv_do_shortcode($content);
    $length = (int)$atts['length'];
    if (strlen($content) > $length) {
        $content = substr($content, 0, $length) . '…';
    // Strip HTML Tags
    $content = strip_tags($content);
    return $content;

I have a problem when an image is placed in the first part of the post content. I think that the function is counting the image as part of the character count.

You can see the effect in the first post 'Now you can get updates about the work we do!' where no text is being displayed.

You can see the full post at hidden link

Is it possible to modify the function to strip out all images before the character limit is applied?

I have a similar problem with image captions which are also being displayed and counted as characters. Is there a way to strip these as well within the function before the character limit is applied?

(By the way, I cannot use the 'post excerpt' field instead as it is used for a special function related to sending posts to Facebook)




Unfortunately, this ShortCode is not related to Toolset features, but WordPress and PHP function only.
We can't assist with debugging or solving issues related to it.

And yes, everything you add to the post body will be considered, that is exactly the purpose of that Code.
It gets the content from $content = wpv_do_shortcode($content);, wpv_do_shortcode is nothing else but like do_shortcode of WordPress.
It executes the $content's shortcodes (so if there are Shortcodes in the content, these are executed)

The whole thing is then counted, and stripped.
You'd have to build a custom $content, to first remove images, then strip/cut the content, then add images again.
That's complex because you won't know where to add the images.


Okay Beda, I'll spend some hours to see if I can find a solution.
I guess you can close this thread now.


Ok, please let me know if I can help with anything else!