Support

Account

Home Forums Front-end Issues Display only field with a content + add code inside a field Reply To: Display only field with a content + add code inside a field

  • Here is a quick and basic tutorial:

    Assuming your pages are using the default page template (page.php in your theme directory on the server) and you only want the field values on certain specific pages, not everywhere:

    1. Make a copy of page.php and name it whatever you like (e. g. page-acf.php)
    2. Open the PHP file in a code editor or a plain text editor of your choice
    3. At the very top of the file, add this code to let the system know that this is a global template:
      
      <?php
      	/*
      		Template Name: Page with custom fields
      		Template Post Type: page
      	*/
      	get_header();
      …
      …
      

      (you can use whatever text you like for the template name)

    4. Add the template code for which John Huebner gave an example after the the_content() function, like so:
      
      the_content();
      if (get_field('proprietario')) {
      ?>
      <strong>PROPRIETARIO:</strong> <?php the_field('proprietario'); ?><br />
      <?php
      }
      if (get_field('indirizzo')) {
      ?>
      <strong>INDIRIZZO:</strong> <?php the_field('indirizzo'); ?><br />
      <?php
      }
      …
      …
      …
      

      and so on and so forth.

    5. In the admin area, go to the ACF field groups and edit the group that contains these fields. Change the location rules to “page template equals Page with custom fields” (or whatever you named your page template)
    6. And lastly, edit the pages where you want the fields to show up and in the sidebar under “Document”, open the “Page attributes” panel and select the template.

    That sounds like a lot but it’s just a one-time setup and then the only thing you need to do is select the appropriate page template whenever you want to show the fields.