将 post-object 与 ACF 中的子字段混合
Mix post-object with sub fields in ACF
我正在尝试创建一个循环,让我可以在页面上显示来自具有常规文本字段(与 post-object 分开)和 post-object 字段的转发器字段的项目。我想从 post-object 中收集标题和特色图片,并从文本字段中收集自定义描述。
这是转发器字段的结构:
highlighted_projects [Repeater]
selected_projects [Post-object]
description [Text]
这是我到目前为止成功显示所选内容的代码 post:
<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"); ?>
放入循环中,它就会中断。我假设它与尝试在连接到 post-object 的 post 中找到 description
字段而不是中继器本身的循环有关。对于如何同时实现 post-object 和相关子字段的循环,我们将不胜感激。
设置 post 对象之前 -
$description = get_sub_field('description')
- 在
之后
$select_project = get_sub_field('selected_project');
然后回显 $description
我正在尝试创建一个循环,让我可以在页面上显示来自具有常规文本字段(与 post-object 分开)和 post-object 字段的转发器字段的项目。我想从 post-object 中收集标题和特色图片,并从文本字段中收集自定义描述。
这是转发器字段的结构:
highlighted_projects [Repeater]
selected_projects [Post-object]
description [Text]
这是我到目前为止成功显示所选内容的代码 post:
<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"); ?>
放入循环中,它就会中断。我假设它与尝试在连接到 post-object 的 post 中找到 description
字段而不是中继器本身的循环有关。对于如何同时实现 post-object 和相关子字段的循环,我们将不胜感激。
设置 post 对象之前 -
$description = get_sub_field('description')
- 在
之后$select_project = get_sub_field('selected_project');
然后回显 $description