user379888
user379888

Reputation:

Advanced Custom Field values not getting displayed

I am using custom fields in 4 places on the blog. I have entered default in the advanced custom field but nothing gets printed.

   <?php
      global $formTitle;
      global $formSubtitle;
      global $formBackground;


        $formTitle = get_post_custom_values( 'title' );


        $formSubtitle = get_post_custom_values( 'description' );


      if (!$formBackground):
        $formBackground = 'contact-form--background';
      endif;

      $portalId = get_post_custom_values( 'portalid' );
      $formId = get_post_custom_values( 'formid' );
    ?>

    <div id="cta__contactForm" class="contact-form <?= $formBackground ?>">
      <div class="page-width text-center">
        <h4 class="contact-form__header"><?= $formTitle ?></h4>
        <p class="contact-form__subheader"><?= $formSubtitle ?></p>
        <!--[if lte IE 8]>
        <script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>
        <![endif]-->
        <script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
        <script>
          // Blog form
          hbspt.forms.create({
            css: '',
            portalId: <?php $portalId?>,
            formId: <?php $formId?>
          });
        </script>
      </div>
    </div>

Upvotes: 0

Views: 287

Answers (1)

Joe
Joe

Reputation: 1812

You should be using the Advanced Custom Fields' function get_field() instead of get_post_custom_values().

It looks like you're using a WordPress function, which doesn't return values from ACF.

You can learn more about get_field() here: https://www.advancedcustomfields.com/resources/get_field/.

I can't really re-write your code to fix the problems, because I don't know what field type each field is, or what format you had it set to save as. But, here's a simple example:

<?php $myCustomField = get_field('customMessage');
echo '<h3>'.$myCustomField.'</h3>'; ?>

This would grab the value of the custom field "customMessage", if set as a text field, and output that text inside of an h3 tag.

Upvotes: 2

Related Questions