Home › Forums › General Issues › Can't get Excerpts to work › Reply To: Can't get Excerpts to work
Hi @cstrasz
If you add this to the functions file:
// Custom Excerpt function for Advanced Custom Fields
function custom_field_excerpt() {
global $post;
$text = get_field('your_field_name'); //Replace 'your_field_name'
if ( '' != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = 20; // 20 words
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters('the_excerpt', $text);
}
Ensure you replace ‘your_field_name’ with the actual ACF field you need to use.
In your template (possibly archive.php), where you need to find the loop and add:
echo custom_field_excerpt();
Depending on your theme/custom code, you may need to adjust where the code is displayed.
For example, you may have the loop which has an include to another file to show the content, so the above line may need adding there instead.
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.