未显示高级自定义字段值

Advanced Custom Field values not getting displayed

我在博客的 4 个地方使用了自定义字段。我在高级自定义字段中输入了默认值,但没有打印任何内容。

   <?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>

您应该使用高级自定义字段的函数 get_field() 而不是 get_post_custom_values()。

看起来您正在使用 WordPress 函数,它没有 return 来自 ACF 的值。

您可以在此处了解有关 get_field() 的更多信息: https://www.advancedcustomfields.com/resources/get_field/.

我真的无法重写您的代码来解决这些问题,因为我不知道每个字段是什么字段类型,或者您将其设置为另存为什么格式。但是,这里有一个简单的例子:

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

如果设置为文本字段,这将获取自定义字段 "customMessage" 的值,并在 h3 标签内输出该文本。