Skip Navigation

[Resolved] Limit number of characters/words output by multiline text field in PHP archive

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

Problem:

The issue here is that the user wanted to limit the word that is being displayed from a custom field in php.

Solution:

The user is currently using this to display his custom field using PHP

<?php if(types_render_field("author-member-new-bio", array('class'=>''))){?>
    <?php echo(types_render_field("author-member-new-bio", array('class'=>''))); ?>
<?php }?>

This can be done by using the wp_trim_words function as you can see from this it can be used to trim the words.

<p class="paragraph-about"><?php echo wp_trim_words( get_field('author_member_new_bio'), 60, '...' ); ?></p>

100% of people find this useful.

This support ticket is created 6 years, 6 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by andrewB-7 6 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#915689

Hi there.

Many thanks for your time, and I hope you can help me.

We have switched a lot of fields on this site to Types from Advanced Custom Fields. Displays of those fields are managed via custom PHP template files.

Since doing so however we've noticed that the multiline text field on archive pages is outputting the entire content, rather than a limit of 60 words, which is how we had it previously.

The archive page should only display 60 words (or around 300 characters), and then the full content be displayed on the single page.

So how do we limit the word/character output on the archive page? The code we are currently using is as follows:

<?php if(types_render_field("author-member-new-bio", array('class'=>''))){?>
	<?php echo(types_render_field("author-member-new-bio", array('class'=>''))); ?>
<?php }?>

...whereas the code we previously used with Advanced Custom Fields looked like this:

<p class="paragraph-about"><?php echo wp_trim_words( get_field('author_member_new_bio'), 60, '...' ); ?></p>

If you could help me to fix this, that would be great!

Many thanks again for your time, and very best wishes,
Andrew.

#915719

I think I found the answer myself, can you check this (it seems to work on the site) to be sure it is correct please:

<?php if(types_render_field("author-member-new-bio", array("raw" => "true"))){?>
	<p class="paragraph-about"><?php echo wp_trim_words( types_render_field("author-member-new-bio", array('class'=>'')), 70, '...' ); ?>
<?php }?>
#915923

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Andrew,

Thank you for contacting our support forum.

Actually yes this is the correct way to do this.

Thanks,
Shane

#915924

Perfect, thanks Shane!