- This topic has 17 replies, 5 voices, and was last updated 2 years, 6 months ago by Elvin.
-
AuthorPosts
-
June 23, 2020 at 11:38 am #1339234Oleksiy
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
June 23, 2020 at 4:30 pm #1339513TomLead DeveloperLead DeveloperHi 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 π
June 24, 2020 at 4:45 am #1340025OleksiyHi 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
June 24, 2020 at 6:27 am #1340106DavidStaffCustomer SupportHi 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.June 24, 2020 at 8:44 am #1340421OleksiyHi David,
I have a website deployed on localhost right now. After moving it to web hosting I will update the topic.
Thank you
June 25, 2020 at 3:24 am #1341167DavidStaffCustomer SupportWe’ll be here π
June 28, 2020 at 1:45 pm #1344811OleksiyTom, 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 π
June 28, 2020 at 1:55 pm #1344834TomLead DeveloperLead DeveloperGlad you got it working! π
September 26, 2021 at 12:55 pm #1943637TylerWould 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!
September 26, 2021 at 2:48 pm #1943736DavidStaffCustomer SupportHi 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.September 26, 2021 at 3:48 pm #1943750TylerThank you! Do I place this code in a code snippet?
September 26, 2021 at 6:29 pm #1943782ElvinStaffCustomer SupportHi 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. π
September 27, 2021 at 8:21 am #1944609TylerI 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!
September 27, 2021 at 4:31 pm #1944937ElvinStaffCustomer SupportCan 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)
September 27, 2021 at 4:45 pm #1944952TylerOf course! Here are my ACF Fields:
and my Post Type (Recipe) edit screen:
and the Code Snippet:
-
AuthorPosts
- You must be logged in to reply to this topic.