在WooCommerce存档页面中的产品标题下方显示ACF字段/图像 [英] Display ACF field/image below the product title in WooCommerce archive pages

查看:36
本文介绍了在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 '<p class="ozel-alanlar">'.$custom_field.'</p>';
    }

}

我也尝试了这段代码来显示图片域,但它不起作用;

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 '<img src="' . $oa2 . '" />';
    }

}

这是结果;

http://www.awesomescreenshot.com/image/3601047/62187bf2b14fc220112fc9456bda1ec9

和我创建的自定义字段;

http://www.awesomescreenshot.com/image/3601051/9559df786631f1dc4d81937487e6bba2

http://www.awesomescreenshot.com/image/3601052/202b5f9b0f05aa661e16ece303ab7abe

如果有人能帮助我,我将不胜感激。

致以最良好的祝愿。

推荐答案

由于您使用的是高级自定义字段,因此需要使用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 '<p class="ozel-alanlar">' . $text . '</p>';
    }

    // Display ACF image
    if( $image_url = get_field( 'oa2', $product->get_id() ) ) {
        echo '<img src="' . $image_url . '" />';
    }
}

代码放在活动子主题(或活动主题)的函数.php文件中。已测试并正常工作。

您将得到如下内容:

ACF image field documentation

这篇关于在WooCommerce存档页面中的产品标题下方显示ACF字段/图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆