如何使用 'post_type' => 'product' 在 WP_Query 循环中显示 ACF
How to show ACF in a WP_Query loop with '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/
这是最终代码,仅供参考
<?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
简单地遍历帖子
foreach ( $loop->posts as $post ) {
$linked_with_items = get_field('linked_with_items', $post->ID);
}
如果 none 有效,请确保您的产品确实具有该自定义字段,仔细检查 ACF 字段设置(规则部分)、字段别名,并仔细检查您的产品编辑页面以查看字段是否显示在那里。
努力做到这一点: 我在我的 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/
这是最终代码,仅供参考
<?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
简单地遍历帖子foreach ( $loop->posts as $post ) {
$linked_with_items = get_field('linked_with_items', $post->ID);
}
如果 none 有效,请确保您的产品确实具有该自定义字段,仔细检查 ACF 字段设置(规则部分)、字段别名,并仔细检查您的产品编辑页面以查看字段是否显示在那里。