Support

Account

Home Forums Backend Issues (wp-admin) Getting Custom Fields to display

Solving

Getting Custom Fields to display

  • I have content that I would like to display on two different pages, with only needing to be able to edit the content in one central location, so someone pointed me towards ACF. I’ve been trying to set it up, but I’m having some poor luck thus far.

    The content are Events. I set up a custom post type of Events and created a Field Group in ACF as well, adding labels for event_name, event_location, event_url, etc. I then went and added 4 Events using the custom fields that were generated (I left the whole WYSIWYG section blank).

    From here, I created a templates folder in my theme. I opened my index.php file and replaced everything between the Loop with then copied the basic usage example from Displaying Custom Field Values in Your Theme starter page of the Documentation, modifying it like so:

    <?php while ( have_posts() ) : the_post(); ?>
    
    			<h1><?php the_field('event_name'); ?></h1>
    
    			<a href="<?php the_field('event_url'); ?>">Test URL</a>
    
    			<p><?php the_content(); ?></p>
    
    		<?php endwhile; // end of the loop. ?>

    I saved that as test.php and uploaded it to the Templates folder. I then created a new blank page and chose the theme I had just made from the dropdown, called the page “Test” and saved it. All that displayed on the page as far (as content goes, I mean my theme showed up of course) was “Test URL” linked to nowhere.

    How do I make it pull from the custom fields I created for Events?

    I also went ahead and replicated this process as well for the code block for “Working with Array values” on the Code Examples page, since I thought maybe I need to use an array since I want to display the list of events and not just one. All that displayed here was “bool(false)”

    At this point, I’m not sure what I’m doing wrong. Is my syntax somehow incorrect? Do I need to save the test.php file as something else? Do I need to reference which post type to look for the custom fields I am referencing? Configure my functions.php in some manner? Or something altogether? I’m not sure what to do to get them (all) to display so I can start styling them appropriately thereafter.

  • Sometimes I run into issues with the detection of the post ID in the default loops. Might try manually passing in the ID and seeing if you get the expected values. Edited code:

    
    <?php while ( have_posts() ) : the_post(); ?>
    
      <h1><?php the_field('event_name', get_the_ID()); ?></h1>
    
      <a href="<?php the_field('event_url', get_the_ID()); ?>">Test URL</a>
    
      <p><?php the_content(); ?></p>
    
    <?php endwhile; // end of the loop. ?>
    
  • Hi @anghellickarma,

    Thanks for the post.

    Perhaps the issue is with the referencing/including of your custom template in your page template. It is also important to ensure that you map the fields to the post page or template using the location rules section.

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

You must be logged in to reply to this topic.