【问题标题】:Wordpress ACF does not retrieve field infoWordpress ACF 不检索字段信息
【发布时间】:2017-02-02 20:55:34
【问题描述】:

我想知道从后端检索信息时遇到了什么问题。 (从来没有遇到过这样的问题)

我制作了一个由 get_template_part() 部分组成的 index.php。 成为

<?php get_header(); ?>


    <?php get_template_part( 'partials/index', 'slider' );
         get_template_part( 'partials/index', 'services' );
             get_template_part( 'partials/index', 'work' ); 
                get_template_part( 'partials/index', 'testimonials' ); 
                     get_template_part( 'partials/index', 'recent' ); 
                         get_template_part( 'partials/index', 'technologies' ); 
                             get_template_part( 'partials/index', 'contact' ); ?>


        <?php get_footer(); ?>

所有这些模板部分都包含了自己的 html+php。 它从这些部分中检索所有 HTML,但不检索 ACF 字段上的信息。

“partials”文件夹中的 index-services.php 示例:

<section id="services">
<div class="container">
    <div class="row">
        <div class="col-lg-12 text-center">
            <div class="title">

                <h1 class="section-heading text-uppercase">
                    <?php  the_field('service_section_title'); ?>
                </h1>

                <p class="text-faded">
                    <?php  the_field('service_section_description'); ?>
                </p>

                <div class="btn btn-primary btn-sm">
                    <a href="<?php  the_field('service_section_button_link'); ?>">Learn more</a>
                </div>

            </div>

        </div>
    </div>
</div>

添加index-services.php的所有链接(带repeater字段)

http://pastebin.com/xXwBf8uG

Image of retrieved HTML of get_template_part()

问题是,这些 the_field() 函数都没有从后端检索信息。

我已将后端字段设置为在页面中显示为索引(并且显示、填充)

我是否遇到了查询循环问题?

【问题讨论】:

    标签: php wordpress wordpress-theming advanced-custom-fields


    【解决方案1】:

    尝试传递帖子 ID:&lt;?php the_field($field_name, $post_id); ?&gt;

    如果你在循环之外,那可能就是问题所在。

    【讨论】:

    • 也许你有时间解释一下?
    • post->ID; ?> 你建议这个?
    • 抱歉,忘记引用代码了。所以用代码查看我上面的答案
    • 在 index-services.php 我猜你正在循环浏览一些帖子,对吧?获取这些帖子 ID
    • 我得到了“77138”(可能是 post_id)。但没有其他显示:/
    【解决方案2】:

    ACF the_field() 函数在循环内部工作。它使用$post-&gt;ID 来获取附加到任何特定帖子或页面的信息。 尝试像 the_field('FIELD_NAME', $post-&gt;ID); 这样传递帖子 ID,或者如果是转发器,您需要在转发器循环中传递帖子 ID:

    if ( have_rows('FIELD_NAME', $post-ID) ){
        while ( have_rows('FIELD_NAME', $post->ID) ): the_row();
            the_sub_field('SUB_FIELD_NAME'); // No need to add post ID here
        endwhile;
    }
    

    我想这会解决你的问题。

    【讨论】:

    • 我的问题是我在循环中使用了 the_field(),应该使用 the_sub_field()
    • @MartinČiurlionis 是的。只是跳过了我的想法。我已经编辑了答案。它可能会帮助其他人。
    猜你喜欢
    • 1970-01-01
    • 2015-12-26
    • 2018-12-13
    • 2021-11-23
    • 2020-04-25
    • 2019-03-30
    • 2016-08-15
    • 1970-01-01
    • 2017-06-20
    相关资源
    最近更新 更多