【问题标题】:Addition of Some Image Next to "Add to Cart" Button在“添加到购物车”按钮旁边添加一些图像
【发布时间】:2014-10-28 02:51:45
【问题描述】:

我需要使用高级自定义字段插件在 WooCommerce 产品页面的“添加到购物车”按钮旁边显示一张图片。据我所知,为了显示值,必须将 the_field 函数添加到某些页面模板中。 the_field 函数应该放在哪个模板文件中?

【问题讨论】:

    标签: wordpress woocommerce advanced-custom-fields


    【解决方案1】:

    这取决于您使用的“添加到购物车”类型(简单产品、变量等)。

    不管怎样,每个都在这个目录下(单品):templates/single-product/add-to-cart

    您应该(为了获得最佳效果)为您的 ACF 内容使用图像数组。

    你可以这样写(把get_field('image')改成get_field('your_field_name')

    <?php 
    
    $image = get_field('image'); // assigns the image field to the variable of $image
    
    if( !empty($image) ): ?> <!--if the $image variable isn't empty, display the following:-->
    
        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /> <!--displays the URL for the image variable and also the alt tag which is entered in the WordPress media library-->
    
    <?php endif; ?> <!--ends the if statement -->
    

    如果您想要一个基本的显示,您可以使用图像 URL 并执行以下操作:

    <?php if( get_field('image') ): ?> <!--only shows the image field if it exists (an image was uploaded through ACF)-->
    
        <img src="<?php the_field('image'); ?>" /> <!--displays the URL of the image if it is not an array/ID set in ACF field parameters, but a URL -->
    
    <?php endif; ?> <!-- end the IF statement -->
    

    我更喜欢尽可能使用 alt 标签。

    【讨论】:

    • 是的!正是我想要的。我的 php 很差,你能给我一个解释你上面写的代码的源代码,以便我可以编辑它吗?
    • 我会将 cmets 添加到上面的内容中。它来自 ACF,但他们希望您了解 PHP。
    猜你喜欢
    • 2017-03-12
    • 2016-09-05
    • 2018-09-30
    • 2021-09-20
    • 1970-01-01
    • 2018-04-12
    • 2018-08-11
    • 2016-09-28
    • 1970-01-01
    相关资源
    最近更新 更多