本文介绍了在WooCommerce存档页面中的产品标题下方显示ACF字段/图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!问题描述
我使用的是高级自定义域插件,我只能在存档页面上的产品标题下方显示文本域,但我还需要在同一位置(文本域下)将图像显示为徽标。
这是我用来显示文本字段的代码,它起作用了;
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
global $product;
// Get the custom field value
$custom_field = get_post_meta( $product->get_id(), 'oa1', true );
// Display
if( ! empty($custom_field) ){
echo ''.$custom_field.' ';
}
}
我也尝试了这段代码来显示图片域PHP图像处理,但它不起作用;
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_image_display_below_title', 2 );
function custom_image_display_below_title() {
global $product;
// Get the custom field value
$oa2 = get_post_meta( $product->id, 'oa2', true );
// Display
if ( ! empty( $oa2 ) ) {
echo '';
}
}
这是结果;
和我创建的自定义字段;
如果有人能帮助我,我将不胜感激。
致以最良好的祝愿。
推荐答案
由于您使用的是高级自定义字段,因此需要使用get_field()专用函数。需要将图像字段设置为"图像URL"。现在代码将为:
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
global $product;
// Display ACF text
if( $text = get_field( 'oa1', $product->get_id() ) ) {
echo '' . $text . ' ';
}
// Display ACF image
if( $image_url = get_field( 'oa2', $product->get_id() ) ) {
echo '';
}
}
代码放在活动子主题(或活动主题)的函数.php文件中。已测试并正常工作。
您将得到如下内容:
ACF image field documentation
这篇关于在WooCommerce存档页面中的产品标题下方显示ACF字段/图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持吉威生活!
[英文标题]Display ACF field/image below the product title in WooCommerce archive pages
(编辑:成都站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|