Home Forums General Issues Trimming words from a field Reply To: Trimming words from a field

  • Apologies, it was an error in my code!

    Add the below to your functions file:

    function project_excerpt() {
    	global $post;
    	$text = get_field('objetivos');
    	if ( $text ) {
    		$text = strip_shortcodes( $text );
    		$text = str_replace(']]>', ']]>>', $text);
    		$text_length = strlen($text); // Get text length (characters)
    		$excerpt_length = 50; // 50 desired characters
    		$excerpt_more = '…';
    		// Shorten the text
    		$text = substr($text, 0, $excerpt_length);
    		// If the text is more than 50 characters, append $excerpt_more
    		if ($text_length > $excerpt_length) {
    			$text .= $excerpt_more;
    	return $text;
    add_shortcode('custom_acf_excerpt', 'project_excerpt');

    This assumes 2 things:
    1) The field name is actually a lower case o (not the label which is probably uppercase O)
    2) It’s a standalone field i.e. not in a flexi layout, gutenberg block etc.
    3) In your Elementor editor, you can add shortcodes

    If you can, simply add

    I’ve just added Objetivos field to a fresh WP install.
    I then added text to the field
    As it’s using the twenty-twentyone theme, I added a shortcode widget into the Gutenberg editor, I then added [custom_acf_excerpt]
    It displayed the truncated text I’d used.