Support

Account

Home Forums General Issues Assign Custom Fields to Post Excerpt

Solving

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');
  • Hi all,

    can we do such thing with plugin like elementor or anything

  • You can’t edit “templates” in the traditional way when using elementor. Your choices are limited. You can get elementor pro and you can create templates. You can include fields using elementor pro widgets using it’s ACF integration. You can create filters in WP to add content to like the excerpt. You can build your own shortcodes. Nothing short of creating custom code will add custom fields to the excerpt in elementor.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Assign Custom Fields to Post Excerpt’ is closed to new replies.