如何在WP_Query循环中显示 ACF 'post_type' => 'product'



尝试实现这一点:我在我的WooCommerce产品中制作了一个ACF(自定义字段(,现在我试图用以下代码在模板中显示这个字段:

<ul class="products">
<?php
$args = array(
'posts_per_page' => 20,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field'    => 'name',
'terms'    => 'grouped',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$linked_with_items = the_field('linked_with_items');
the_title('<strong>', '</strong>'); echo '<br />';
echo $linked_with_items;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->

但是无论我用get_field((尝试什么,该字段都不会显示在我的模板中。有人能帮忙吗?https://www.advancedcustomfields.com/

这是最后一个代码fyi

<?php if( have_rows('catalogue') ): ?>
<?php
while( have_rows('catalogue') ): the_row(); // catalogue is the field
the_sub_field('linked_with_items'); ?>
<?php endwhile; ?>
<?php endif; ?>

你可以试试这个:

$linked_with_items  = get_field('linked_with_items', get_the_ID());

如果这不起作用,就像测试一样,你可以试着用前臂简单地循环帖子

foreach ( $loop->posts as $post ) {
$linked_with_items  = get_field('linked_with_items', $post->ID);
}

如果这些都不起作用,请确保您的产品确实有该自定义字段,仔细检查ACF字段设置(规则部分(、字段段塞,并仔细检查您的产品编辑页面,查看字段是否显示在那里。

相关内容

最新更新