Site logo

Archived topic

Add ACF custom fields to a post excerpt

17 replies · Started by Oleksiy on June 23, 2020

Viewing posts 1–15 of 18

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

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 :)

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

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.

Hi David,

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

Thank you

We'll be here :)

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 :)

Glad you got it working! :)

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!

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.

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

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!

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)

Of course! Here are my ACF Fields:
acf

and my Post Type (Recipe) edit screen:
post

and the Code Snippet:
snippet

This archived topic is closed to new replies.