ACF 未在 wp 主题的页脚中显示值

ACF not displaying the value in the footer of the wp theme

这看起来很简单,但我无法让它工作。我想要的只是显示一个存储为自定义字段的标题(它是 h3 标签)。我在我的代码中的另一个地方使用它并且它有效,但不在页脚中。 ACF 是否只在某些领域有效,而在其他领域无效?

<footer class="section section--footer">
    <div class="section--footer__header">
$post_id = get_queried_object_id();
        <h3 class="text-white"><?php echo the_field("cta_field_title",$post_id);?></h3>
    </div>
    <form action="#" class="section--footer__form">
        <div class="section--footer__input-box center">
            <label for="name"></label>
            <input type="text" id="name" placeholder="name" class="input">
            <label for="email"></label>
            <input type="text" id="email" placeholder="email address" class="input">
        </div>
        <div class="button__box center">
            <a href="#" class="button">Submit</a>
        </div>
    </form>
    <div class="section--footer__links">
        <?php wp_nav_menu(array(
            "theme_location" => "footer",
            "menu" => "desktop",
            "menu_class" => "section--footer__links"
        )) ?>
        <?php wp_nav_menu(array(
            "theme_location" => "social",
            "menu" => "desktop",
        )) ?>
    </div>

</footer>

<?php wp_footer() ?>

</body>

</html>

当您使用 ACF 函数在 Wordpress“循环”之外获取字段值时,您需要将 post id 作为第二个参数传递给函数,例如

$post_id = get_queried_object_id(); // gets the id of the current page/post
the_field("cta_field_title", $post_id);

(仅供参考,您不需要将 echo 与 the_field 一起使用...该函数已显示该值。但是您需要将其与 get_field、[=14= 一起使用] 等)。

因此您的页脚将如下所示:

<footer class="section section--footer">
    <div class="section--footer__header">
        <?php $post_id = get_queried_object_id();  /* get the current page/post id */ ?>
        <h3 class="text-white"><?php the_field("cta_field_title", $post_id);?></h3>
    </div>
    // rest of your code here
</footer>

参考文献: