Support

Account

Home Forums General Issues Can't get Excerpts to work

Solving

Can't get Excerpts to work

  • Hello,

    I am using both ACF and Elementor Pro, and setting up a simple blog posting form for some members of my team. I do not have much code experience so I’m working through tutorials to learn the system.

    One issue I’m facing is that the automated excerpts are not showing up in the archive page. I’ve googled and seen others mention that functions need to be added to retrieve the custom field for the excerpt to display. I have not had any of these solutions work and they all date pretty far back, so I’m not sure if I’m doing something wrong or if the WP core code has since changed.

    Examples of what I’ve tried include:

    https://support.advancedcustomfields.com/forums/topic/field-to-write-into-the_content-the_excerpt/

    https://gist.github.com/Sanabria/6a8ceb1596b3fd0b1f805ed2c47814d0

    I would appreciate any help with figuring out how to make this work :). Thanks!

  • 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.

  • Hi Jarvis,

    I have the same problem of the first post, I did the following:
    I have pasted this code in functions.php:

    function custom_field_excerpt() {
    global $post;
    $text = get_field(‘extracto’);
    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);
    }

    extracto is the name of the field

    and pasted this, in archive.php

    echo custom_field_excerpt();

    but it doesn’t work… the default post element doesn’t recognize this field as the excerpt, I don’t know what is wrong, the field itself must contain some special type or something? right now is a common text field.

    Help please!

  • I solved, I have found and tried this and works:

    This should work for you. Make the field name ‘the_content’. I am wrapping this up into a plugin and will also include the_excerpt, and featured_image.

    paste this in functions.php:

    function hg_acf_save_content( $value, $post_id, $field )
    {
    wp_update_post( array( ‘ID’ => $post_id, ‘post_content’ => $value ) );
    return $value;
    }
    add_filter(‘acf/update_value/name=the_content’, ‘hg_acf_save_content’, 10, 3);

    the only important thing is to name your desired field as:

    the_content

    and works!

  • Thas for solving this, I had the same issue. You saved my ass Thanks 😀

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.