Home › Forums › General Issues › Assign Custom Fields to Post Excerpt
Hello,
I’m using ACF on my WordPress website that I’m currently developing. I’ve been searching for a while now for a solution to a small issue and I’m wondering if anyone could help. I have a simple layout on my site and am using Elementor Pro.
My homepage lists all recent posts and each one shows the usual Title, Image, ‘Read More’ link and the basic meta data such as Author and Date.
I’m trying to add two custom fields to each post preview. This would be a ‘Price’ and ‘Link button’. I’ve included an image as an example. Using AFC I’ve managed to get the price to show no problem. However I’m unsure how to go about adding a custom ‘Link’ Button. This button link would be different for each post (each post is for a different website).
The closest site as an example of what I’m trying to achieve would be http://www.shutupandtakemymoney.com/
As you can see, a very simple layout which when you create a post, you can input it’s own price and button link. If this wasn’t possible, would there be a way to link a AFC to the ‘Read More’ button so every link could be customised?
Sorry if this sounds confusing, I’ve not had much luck with google or any other forums!
Thank You for your time reading this 🙂
It should be possible by simply modifying the template file that displays your home page, and that is best done in a Child Theme unless your Theme is completely custom and you don’t have to worry about template files being overwritten by Theme updates.
SO first identify your Child Theme’s home page template (many themes use different names such as homepage.php, frontpage.php, or even index.php – you’ll have to figure out which one it is, if you’re having trouble way at the bottom I have a snippet you can add that tells you which template file is being used on any page).
Then find the spot in the template where the_excerpt(); appears – MANY newer Themes use a ‘content’ loop so the template MAY refer you to another template (part) like this:
get_template_part( 'content','specific-content-type' );
If it does then open up that content file and look for the_excerpt(); there.
Then all you have to do is insert your custom field variables either before or after the excerpt, or even replace the excerpt with your custom field data if you don’t want to use an actual excerpt (say for a short product description, which you can either pull from the_content(); or use the actual excerpt field on the Post Edit screen, if it’s not showing make sure it’s checked under “Screen Options” at the top of the page)
If you need help in how to add your custom field data to the template, let me know and I can give you some code.
— IF YOU CAN’T figure out what template file is being used, add THIS CODE to your Child Theme’s functions.php file OR your own custom plugin, you can comment it out or uncomment as necessary, or just leave it uncommented all the time – this will ONLY display the template file used IF you are a logged-in Admin-level user, it shows at the TOP of the page above the header the path & file name:
// Debug function to show the name of the current template being used
function show_template() {
if ( current_user_can( 'create_users' ) ) {
global $template;
print_r($template);
}
}
add_action('wp_head', 'show_template');
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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.