Skip Navigation

[Resolved] Limit post body content by words, cannot use Exerpt because content has an link

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

Problem: How to limit post body content to given the number of words. For example, display only 25 words from post content.

Solution: To limit the post body content to certain given number of words and display it you need to add the custom shortcode to your functions.php file that fetches your post content and returns the given number of words.

You can find the suggested solution with the following reply: https://toolset.com/forums/topic/limit-post-body-content-by-words-cannot-use-exerpt-because-content-has-an-link/#post-360425

Relevant Documentation:

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.

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by Pastor Walt 5 years, 10 months ago.

Assigned support staff: Minesh.

Author
Posts
#359712

I am trying to: display a limited number of words from the post-body I cannot use the excerpt because the body contains a link I want displayed. I tried using the parameters I use with the excerpt but that did nothing.

I visited this URL:

I expected to see:

Instead, I got:

#359735

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I think you should create a custom shortcode to display your content.

Please try to follow following steps:

1)
Try to add following code to your functions.php file.

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;
}

2)
Try to call your shortcode as given under:

[limit_content length="140"]
[types field="manual-excerpt" output="raw"][/types]
[/limit_content]

Just try to pass your original expert field.

Does this helps?

#359868
Screenshot 2016-01-16 10.49.55.png

I tried that solution and got nothing returned. I didn't understand how it would work with the field to be limited not being mentioned anywhere but I tried it anyhow.

I since have altered the code to attempt to include that an make the shortcode able to handle the shortening of any field.

All I get back is the field name shortened.

Here is the shortcode PHP code which is included in the functions.php program

<?php 
add_shortcode('limit_content', 'trim_shortcode');
 function trim_shortcode($atts, $content = '') {
    $fieldname = $atts['field'];
    echo "<br>Field Name = ".$fieldname;
    $content=$fieldname;
    $fielddata = wpv_do_shortcode($content);
    echo "<br>Content Field Data = ".$fielddata;
    $length = (int)$atts['length'];
    echo "<br>Length = ".$length;
    if (strlen($fielddata) > $length) {
        $content = substr($content, 0, $length) . '…';
    }
    return $content;
}
?>
[limit_content length="2" field="wpv-post-body"]
[types field="manual-excerpt" output="raw"][/types]
[/limit_content]

As you can see from the attached screen short what is happening is the field name is simply being shortened, in this case to 2 characters. The full body description is write under the heading. The shortened version is under the social icons.

Thanks for your continued assistance.

#360068

Minesh
Supporter

Languages: English (English )

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

I've altered the code you have send to me in your previous reply.

add_shortcode('limit_content', 'trim_shortcode');
 function trim_shortcode($atts, $content = '') {
	global $post;
		
	$content_post = get_post($post->ID);
	$content = $content_post->post_content;
	$fielddata = apply_filters('the_content', $content);
    $length = (int)$atts['length'];
    if (strlen($fielddata) > $length) {
        $content = substr($content, 0, $length) . '…';
    }
    return $content;
}

How to use:

[limit_content length="25"]

Does this helps?

#360183

The original code for the shortcode failed to execute halting the site.

I modified it as follows

<?php 
add_shortcode('limit_content', 'trim_shortcode');
 function trim_shortcode($atts, $content = '') 
 {
  global $post;
  $post_data = get_post($post->ID);
  $postcontent = $post_data->post_content;
  $fielddata = apply_filters('the_content', $postcontent);
//echo "<br><br>Field Data = ".$fielddata;
  $length = (int)$atts['length'];
//echo "<br>Length = ".$length;
  if (strlen($fielddata) > $length) 
    {
     $trimmedcontent = substr($fielddata, 0, $length) . '…';
    }
  else
    {
    $trimmedcontent=$fielddata;
    }

//echo "<br><br>Trimemd Content = ".$trimmedcontent;
$content=$trimmedcontent;
}
return $content;
?>

This code does trim the post content as verified by the echos when not commented out.
However the shortcode is not displaying the trimmed data. In fact, nothing is shown when using the short code.

[limit_content length="350"]

#360425

Minesh
Supporter

Languages: English (English )

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

The reason behind that is the return statement is outside the shortcode function.

Could you please try following code. I've put return statement before end bracket "}":

add_shortcode('limit_content', 'trim_shortcode');
 function trim_shortcode($atts, $content = '') 
 {
  global $post;
  $post_data = get_post($post->ID);
  $postcontent = $post_data->post_content;
  $fielddata = apply_filters('the_content', $postcontent);
//echo "<br><br>Field Data = ".$fielddata;
  $length = (int)$atts['length'];
//echo "<br>Length = ".$length;
  if (strlen($fielddata) > $length) 
    {
     $trimmedcontent = substr($fielddata, 0, $length) . '…';
    }
  else
    {
    $trimmedcontent=$fielddata;
    }
 
//echo "<br><br>Trimemd Content = ".$trimmedcontent;
$content=$trimmedcontent;
return $content;
}
#360530

I've got it working now. Thanks for your assistance!!

As always, top notch support!!!!!