[Resolved] Add ACF custom fields to a post excerpt

Home Forums Support [Resolved] Add ACF custom fields to a post excerpt

Home Forums Support Add ACF custom fields to a post excerpt

Viewing 15 posts - 1 through 15 (of 18 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

    We’ll be here πŸ™‚

    #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

    Glad you got it working! πŸ™‚

    #1943637
    Tyler

    Would you be able to provide instructions for adding this code to my site? I’m a newbie with very little PHP experience but I’m trying to populate my excerpt field with a ACF (taxonomy) field value. I appreciate any guidance you can provide!

    #1943736
    David
    Staff
    Customer Support

    Hi there,

    Try this method which i have commented // to provide some guidance on the method:

    add_filter( 'the_excerpt', function( $excerpt ) {
        // Get the current taxonomy term
        $term = get_queried_object(); 
        // get the field named your_custom_field
        $the_custom_field = get_field('your_custom_field_name', $term);
        // return the excerpt followed by the_custom_field value
        return $excerpt . $the_custom_field; 
    } );

    Thing to note – this: your_custom_field_name has to be changed to the name of your custom field.

    This does depend on a lot of things though, such as what type of field your using. And you may need to get some advice from ACF to make sure the above get_field method applies.

    #1943750
    Tyler

    Thank you! Do I place this code in a code snippet?

    #1943782
    Elvin
    Staff
    Customer Support

    Hi Tyler,

    That’s right. Here’s our recommended ways of running PHP
    https://docs.generatepress.com/article/adding-php/

    It’s basically a choice between Code Snippets plugin or a child theme’s functions.php. πŸ˜€

    #1944609
    Tyler

    I really appreciate your help. Unfortunately it’s still not working for me. I realize it could be any number of things not working properly but I’m committed to finding a solution. I wonder if the issue may be that I’m using this with a Custom Post Type, not a standard WP Post? I understand if you’re not able to provide continued support on this topic and thank you for your valued feedback!

    #1944937
    Elvin
    Staff
    Customer Support

    Can you share the exact code you’re using and where you’re placing it?

    Screenshots will give us a better idea. (Perhaps there are typographical errors in the code. i.e., wrong ACF slug)

    #1944952
    Tyler

    Of course! Here are my ACF Fields:
    acf

    and my Post Type (Recipe) edit screen:
    post

    and the Code Snippet:
    snippet

Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.