Skip Navigation

[Résolu] Set/limit number of words or character for custom field output on front-end grid

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 1 réponse, has 2 voix.

Last updated by Waqar Il y a 1 mois et 1 semaine.

Assigned support staff: Waqar.

Auteur
Publications
#2086327

Tell us what you are trying to do?

I'm developing a directory site that will take input from user forms on the front-end. One of the custom fields in this form is for a bio (or introduction) section. I used a custom field for this rather than the standard WordPress content field because I don't want the user to have access to the wysiwyg editor; I want to keep content uniform...

The challenge I have is, when I go to create my front-end grid for displaying directory results, I'm unable to limit the number of words/characters that are output from the (bio/intro) custom field. I'd like these blocks to only show ~20 words, just to keep the grid clean. Basically, I'd like to control this custom field's output similar to the way we can configure the standard WordPress excerpt. Is there a way to do that? If not, is there a way for me to use the standard WordPress content field without the user seeing/accessing all the styling options?

Thanks,
Eric

#2088127

Waqar
Supporter

Languages: Anglais (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To limit the output from any field to a certain number of words, you can register a custom shortcode:


function word_trim_custom_func( $atts, $content = null ) {
	$a = shortcode_atts( array(
		'words' => '20',
		'more' => '...',
	), $atts );

	return wp_trim_words( apply_filters('the_content', $content), $a['words'], $a['more'] );
}
add_shortcode( 'word_trim_custom', 'word_trim_custom_func' );

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

After that, you can use this new shortcode in your template through a "Fields and Text" block, like this:


[word_trim_custom words="40" more="...."][types field='post-text-area'][/types][/word_trim_custom]

Please note, how I've used the Toolset Type fields shortcode to show the content from a "post-text-area" field, inside the new custom shortcode ( [types field='post-text-area'][/types] ) and set the limit of words to 40 and the more text that is appended at the end to '....'.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar