【发布时间】:2021-03-09 12:52:05
【问题描述】:
尝试实现这一点: 我在我的 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; ?>
【问题讨论】:
-
只是为了确认您已在您的产品帖子中为这个新字段设置了一个值,对吗?
var_dump(get_field('linked_with_items'));有什么用? -
NULL 是我得到的响应
标签: wordpress advanced-custom-fields