【问题标题】:ACF not displaying the value in the footer of the wp themeACF 未在 wp 主题的页脚中显示值
【发布时间】:2020-07-01 08:21:01
【问题描述】:

这看起来很简单,但我无法让它工作。我想要的只是显示一个存储为自定义字段的标题(它是 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>

【问题讨论】:

    标签: php html wordpress advanced-custom-fields


    【解决方案1】:

    当您使用 ACF 函数在 Wordpress“循环”之外获取字段值时,您需要将帖子 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_fieldget_fields 等一起使用)。

    所以你的页脚看起来像这样:

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

    参考资料:

    【讨论】:

    • 太棒了!非常感谢。我只是好奇为什么它在某些地方而不是其他地方有效。我在其他地方使用 'echo get_field("field name") 并且效果很好。
    • 很高兴为您提供帮助!您需要在 Wordpress "loop" 内才能使其在没有帖子 ID 的情况下工作。循环是&lt;?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?&gt; 和您的endwhile; endif; 之间的代码,它设置了帖子信息,因此可以在没有帖子ID 的情况下访问它。除此之外,the_titlethe_content 等特定于循环的函数不起作用。 (实际上它有点复杂,因为如果全局$post 变量设置在其他地方,它们也可以工作,但这是基本思想!)
    • 所以,现在我在其他页面模板中调用我的页脚 (get_footer()),并且该字段不再显示!我已经尝试添加循环和 $post_id,但它仍然不会显示。有什么想法吗?
    • @jpaulMolnar 不,不要在页脚中添加循环!您只需要获取帖子 ID 并将其传递给 the_field。您是否像上面的代码一样使用get_queried_object_id 并且没有获取帖子ID?
    • 那么您需要更新该问题中的代码以显示您正在使用的代码,否则帮助您的人正在处理错误的代码!显示在您的主页页脚中有效而在其他页面中无效的确切页脚代码。您是否打印出帖子 ID 的值来检查它?在页脚&lt;div class="section--footer__header"&gt; 之后添加此内容:&lt;?php echo "&lt;h1&gt;POST ID=".$post_id."&lt;/h1&gt;"; ?&gt; 并告诉我主页页脚中显示的内容以及页面页脚无法正常工作的内容。
    猜你喜欢
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2021-08-07
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多