将后对象与ACF中的子字段混合

问题描述 投票:1回答:1

我正在尝试创建一个循环,让我显示来自转发器字段的项目,该字段具有常规文本字段(与后对象分开)和页面上的后对象字段。我想从post-object中收集标题和特色图像,并从文本字段中收集自定义描述。

这是转发器字段的结构:

highlighted_projects [Repeater]
   selected_projects [Post-object]
   description [Text]

这是我到目前为止成功显示所选帖子内容的代码:

<section class="bg-white">
  <?php if( have_rows('highlighted_projects')): // check for repeater fields ?>
    <?php $i = 0; ?>
      <?php while ( have_rows('highlighted_projects')) : the_row(); // loop through the repeater fields ?>
        <div class="grid">
      <?php // set up post object
          $select_project = get_sub_field('select_project');
            if( $select_project ) :
            $post = $select_project;
            setup_postdata($post);
            ?>
            <?php if($i % 2 == 0) : ?>
          <div class="grid__col grid__col--3-of-6"><h3><?php the_title(); ?></h3></div>
          <div class="grid__col grid__col--3-of-6"><?php the_post_thumbnail(); ?></div>
        <?php else : ?>
          <div class="grid__col grid__col--3-of-6"><?php the_post_thumbnail(); ?></div>
          <div class="grid__col grid__col--3-of-6"><h3><?php the_title(); ?></h3></div>
        <?php endif; ?>
        <?php $i++; ?>
          <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
      <?php endif; ?>
      </div>
      <?php endwhile; ?>
  <?php endif; ?>
</section>

但是一旦我把<?php the_sub_field("description"); ?>放在循环中,它就会中断。我假设它与循环有关,试图在连接到后对象的帖子中找到description字段而不是转发器本身。非常感谢有关如何同时实现后对象和相关子域的循环的任何想法。

php wordpress advanced-custom-fields
1个回答
1
投票

在设置帖子对象之前 -

$description = get_sub_field('description') 

- 就在之后

$select_project = get_sub_field('selected_project'); 

然后只是回显$ description

© www.soinside.com 2019 - 2024. All rights reserved.