Home › Forums › General Issues › Trimming words from a field
Please help!
I am not anywhere a good coder, but I have found a function to place in my functions.php file to be able to trim a certain amount of words from a custom field. The function is:
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 apply_filters(‘the_excerpt’, $text);
}
And I understand I have to call it from my template.php file like this:
<?php echo project_excerpt(); ?>
My issue is that the field is sitting on the single post template created by ELementor in my site and this template is nowhere to be found as I understand it is “dynamically” created. ANyone can shed a bit of light on where I must be plancing the function call? with a shortcode in the ELementor Single Post?
Very lost here.
Thank you in advance for any insight..
No idea if this would work but…
Convert your function to a custom shortcode:
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 apply_filters('the_excerpt', $text);
}
add_shortcode('custom_acf_excerpt', 'project_excerpt');
You can then either call it as code:
<?php echo do_shortcode("[custom_acf_excerpt]"); ?>
Or as a shortcode in the editor:
[custom_acf_excerpt]
Code is untested
HI Jarvis.
Thank you very much for spending some time on this but this is not working for me..
Any other ideas?
Any help would be much appreciated. I am really lost as to how to proceed… I have been searching the web for hours to no avail, but not being a coder is resulting impossible to understand..
Thanks again.
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
[custom_acf_excerpt]
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.
Wow Jarvis,
You are very kind to be helping out on this. I really appreciate it.
I have had to leave my work for a couple of hours but will test this as soon as I arrive.
In the meanwhile, I just wanted to note that the field I am targetting (Objetivos) is entered dynamically through the ACF selector on a template page that loads my posts in WP with Elementor. The field is a regular text widget that pulls the text dynamically from the ACF in each post item.
Hope this all makes sense.
Thanks again.
hmm…
just tested. Not working here.. But I found that the field name was actaully lowercase. The label had a capital O.
Now with this change, the text field is disppearing completely..
Not sure what is happenning..
got it!
Not quite sure what was not working, but I finally got it working.
THANK YOU JARVIS!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.