【问题标题】:Rendering an image on a WooCommerce product page在 WooCommerce 产品页面上呈现图像
【发布时间】:2015-03-24 08:10:25
【问题描述】:

使用高级自定义字段插件,我试图在 WooCommerce 中的单个产品页面上呈现图像。

我从一个做同样事情的人那里找到了这个 sn-p 代码,但它正在渲染一个文本字段。我将如何更改它以呈现图像?

add_action( 'woocommerce_single_product_summary', 'woocommerce_template_top_category_desc', 1 );
    function woocommerce_template_top_category_desc (){
    $terms = get_the_terms( $post->ID, 'wc-attibute-class' );
    if ( !empty($terms)) {
        $term = array_pop($terms);
                $text= get_field('txt-field', $term);
                if (!empty($text)) {
                echo $text;
                }
}
}

到目前为止,我有这个,但它不起作用。 add 操作是正确的,因为我将它用于其他用途,但从 $terms 开始是我迷路的地方,显然是不对的。

add_action( 'woocommerce_product_thumbnails' , 'add_below_featured_image', 9 ); 
    function add_below_featured_image() {
    $terms = get_the_terms( $post->ID, '496' );
    if ( !empty($terms)) {
        $term = array_pop($terms);
                $image = get_field('banner_feedback', $term);
                if (!empty($image)) {
                echo $image;
                }
}
}

【问题讨论】:

    标签: wordpress woocommerce field custom-fields advanced-custom-fields


    【解决方案1】:

    想通了。

    add_action( 'woocommerce_product_thumbnails' , 'add_below_featured_image', 9 ); 
    function add_below_featured_image() {
        if( is_single( pageidhere ) ) {
        echo '<br><img src="urlhere">';
    }
    }
    

    【讨论】:

      【解决方案2】:

      在woocommerce单品页面的特色图片下方添加自定义图库的步骤(您可以适当修改以仅添加单个图片):

      1. 为图片库创建 ACF。
      2. 将product-image.php 的模板从/plugins/woocommerce/templates/single-product 复制到themes/your-child-theme/woocommerce/single-product/
      3. 在 product-image.php 的结束 div 之前添加以下代码 “图片”
      4. 将您想要在图库中添加的图像添加到 ACF 自定义字段中的单个产品管理页面。

      `

      <?php $images = get_field('product_image_gallery'); ?>
      <div class="mycustom_image_gallery">
      <?php if($images): ?>
          <div class="popup-gallery">
                  <?php foreach( $images as $image ): ?>
                      <a href="<?php echo $image['url']; ?>"
                         class="lightbox-link"
                         title="<?php echo $image['caption']; ?>"
                         data-description="<?php echo $image['description']; ?>">
                          <div class="image-wrap">
                              <img src="<?php echo $image['sizes']['thumbnail']; ?>">
                          </div>
                      </a>
                  <?php endforeach; ?>
          </div>
      
      <?php endif; ?>
      </div>
      

      `

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-10
        • 2021-11-10
        • 2015-01-04
        • 1970-01-01
        • 2014-05-06
        • 2014-09-21
        • 1970-01-01
        • 2014-02-24
        相关资源
        最近更新 更多