[Resolved] Add ACF custom fields to a post excerpt

Home Forums Support Add ACF custom fields to a post excerpt

  • This topic has 7 replies, 3 voices, and was last updated 3 months ago by Tom.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1339234
    Oleksiy

    Hello,

    I’d like to automatically place custom fields inside post excerpt. The fields are created by Advanced Custom Fields plugin. I discovered Elements -> Hooks, but couldn’t find any ones related to excerpt area. Is it possible to do that? I have a lot of posts and don’t want to do it manually.

    Thanks a lot

    #1339513
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    If you want to attach something to an excerpt, you need to filter it like this:

    add_filter( 'the_excerpt', function( $excerpt ) {
        $your_field = get_post_meta( get_the_ID(), 'your_field', true );
    
        return $excerpt . $your_field;
    } );

    Hope this helps 🙂

    #1340025
    Oleksiy

    Hi Tom,

    Thank you for the quick response. I tried to use your code via Code Snippets plugin, but unfortunately it didn’t show me any fields, nothing at all. Am I wrong and should place this code in some other place?

    There is a code I use in Elements -> Hooks and it works good, but can’t show fields inside excerpts exactly:

    <p>Field name 1: <?php the_field('field-id1'); ?></p>
    <p>Field name 2: <?php the_field('field-id2'); ?></p>

    where field-id1, field-id1 – custom field ids from ACF plugin.

    Could you please in summary explain me how to create add_filter() analog and where exactly post the code?

    Many thanks

    #1340106
    David
    Staff
    Customer Support

    Hi there,

    can you share a link to your site, with Tom’s code added, so we can take a look ?
    You can edit your original topic and use the Site URL field to share the link the privately.

    #1340421
    Oleksiy

    Hi David,

    I have a website deployed on localhost right now. After moving it to web hosting I will update the topic.

    Thank you

    #1341167
    David
    Staff
    Customer Support
    #1344811
    Oleksiy

    Tom, David,

    I have fixed the issue. The Tom’s code works fine, it was the fault from my end.

    Many thanks for you support and fantastic Generate Press theme, especially Pro version 🙂

    #1344834
    Tom
    Lead Developer
    Lead Developer
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.