[英]Adding ACF to Woocommerce ul.products on Shop Page
I'm using WooCommerce and Advanced Custom Fields, where ACF group is set up with post type for products.我正在使用 WooCommerce 和高级自定义字段,其中 ACF 组设置为产品的帖子类型。 I would like to add a couple of simple text fields to the products box below the product title, and it will display on all products.我想在产品标题下方的产品框中添加几个简单的文本字段,它将显示在所有产品上。
I believe the hook for this is woocommerce_after_shop_loop_item_title
我相信这个钩子是woocommerce_after_shop_loop_item_title
Image attached for visual description.附加图像用于视觉描述。
Like this, I'm looking to add Address value ( $location
), Bedrooms value ( $bed
) Bathrooms ( $bath
).像这样,我希望添加地址值 ( $location
)、卧室值 ( $bed
) 浴室 ( $bath
)。
I've made an attempt, however I am unsure how to extract the field data from the product's post.我已经尝试过了,但是我不确定如何从产品的帖子中提取字段数据。 Ie I' don't think get_field or get_field_object is enough.即我不认为 get_field 或 get_field_object 就足够了。
Any pointers would really be much appreciated.任何指针都会非常感激。
Thank you in advance.先感谢您。
add_action( 'woocommerce_after_shop_loop_item_title', 'woo_products_property', 1 );
function woo_products_property() {
?>
<div class="property_loop_bottom_sec">
<?php $location = get_field_object('address'); ?>
<?php if( ! empty( $location ) ) { ?>
<div class="feature">
<div class="value"><i class="et-pb-icon map-marker"></i><?php echo $location['value'];?></div>
</div>
<?php } ?>
<?php $bed = get_field_object('bedroom'); ?>
<?php if( ! empty( $bed ) ) { ?>
<div class="feature">
<div class="value"><i class="fas fa-bed"></i><?php echo $bed['value'];?></div>
<span>Bed</span>
</div>
<?php } ?>
<?php $bath = get_field_object('bathroom'); ?>
<?php if( ! empty( $bath ) ) { ?>
<div class="feature">
<div class="value"><i class="fas fa-bath"></i><?php echo $bath['value'];?></div>
<span>Bath</span>
</div>
<?php } ?>
</div>
<?php
} ?>
I revised your code.我修改了你的代码。 Try the below code.试试下面的代码。
function woo_products_property() {
global $post;
?>
<div class="property_loop_bottom_sec">
<?php
$location = get_field( 'address', $post->ID );
if( ! empty( $location ) ) { ?>
<div class="feature">
<div class="value"><i class="et-pb-icon map-marker"></i><?php echo $location['value'];?></div>
</div>
<?php }
$bed = get_field( 'bedroom', $post->ID );
if( ! empty( $bed ) ) { ?>
<div class="feature">
<div class="value"><i class="fas fa-bed"></i><?php echo $bed['value'];?></div>
<span>Bed</span>
</div>
<?php }
$bath = get_field( 'bathroom', $post->ID);
if( ! empty( $bath ) ) { ?>
<div class="feature">
<div class="value"><i class="fas fa-bath"></i><?php echo $bath['value'];?></div>
<span>Bath</span>
</div>
<?php } ?>
</div>
<?php }
add_action( 'woocommerce_after_shop_loop_item_title', 'woo_products_property', 1 );
Tested and works测试和工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.