Skip Navigation

[Resuelto] Limit post body content by words

This support ticket is created hace 6 años, 9 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Este tema contiene 3 respuestas, tiene 2 mensajes.

Última actualización por miguelG-5 hace 6 años, 9 meses.

Asistido por: Christian Cox.

Autor
Mensajes
#620611

I found this code on the forum,

https://toolset.com/forums/topic/limit-post-body-content-by-words-cannot-use-exerpt-because-content-has-an-link/

it does the job, trimming the content on carachters [limit_content length="280"]; is it possible to trim in words ?

Thanks in advance for your help

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

#620613

This is the page i'm working on : enlace oculto

#620685

Unfortunately I don't have a simple snippet on hand to extend this code so that it can count words instead of characters. It's not a trivial process to count words because HTML tags may contain spaces, and you have to account for that programmatically. A quick search online led me to this post, which includes some custom code examples you can try:
https://www.pjgalbraith.com/truncating-text-html-with-php/

#621406

Thank you Christian

Following yous advice, I modified the code, I'm still counting caracteres, but preventing words to be chopped in half

Here is the code to insert in functions.php if someone needs it

----------------------
/*trim custom field */
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) {
$endpos = strpos(str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $fielddata), ' ', $length);
if($endpos !== FALSE)
$trimmedcontent = substr($fielddata, 0, $endpos) . '…';
}
else {
$trimmedcontent=$fielddata;
}
//echo "<br><br>Trimemd Content = ".$trimmedcontent;
$content=$trimmedcontent;
return $content;
}
----------------------------
and the shortcode:

[limit_content length="280"]