【问题标题】:product category image in custom WooCommerce theme自定义 WooCommerce 主题中的产品类别图像
【发布时间】:2012-10-21 14:03:30
【问题描述】:

我正在寻找一些帮助,以查找将在自定义 WooCommerce 主题中显示产品类别图像的 php sn-p。我正在使用一个在小部件中执行 php 代码的插件,它适用于产品类别名称。我只是找不到任何适用于类别图像的东西。任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    假设您知道类别 ID,并且它位于 $cat_ID

    // get the thumbnail ID
    $thumbnail_id = get_woocommerce_term_meta( $cat_ID, 'thumbnail_id', true ); 
    // get the image URL
    $image = wp_get_attachment_url( $thumbnail_id ); 
    // print the IMG HTML
    echo '<img src="'.$image.'" />';
    

    【讨论】:

    • 感谢您的回复。我对此很陌生。您知道检测类别的方法吗?我希望所有类别页面都有一个侧边栏。使用&lt;h1 itemprop="name" class="product_title entry-title"&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt; 正确填充的标题
    • 这样就可以了,然后可以使用$term-&gt;term_id获取类别ID:ericwijaya.wordpress.com/2012/02/16/…
    【解决方案2】:

    要在archive-product.php中显示当前显示类别的类别图像,当is_product_category()为真时使用当前类别term_id:

    // verify that this is a product category page
        if (is_product_category()){
            global $wp_query;
            // get the query object
            $cat = $wp_query->get_queried_object();
            // get the thumbnail id user the term_id
            $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ); 
            // get the image URL
            $image = wp_get_attachment_url( $thumbnail_id ); 
            // print the IMG HTML
            echo '<img src="'.$image.'" alt="" width="762" height="365" />';
        }
    

    【讨论】:

      【解决方案3】:

      参考以下代码:-

      global $product;
      
      if (is_product_category()) {
      
          global $wp_query;
      
          $cat = $wp_query->get_queried_object();
      
          $thumbnail_id = get_woocommerce_term_meta($cat->term_id, 'thumbnail_id', true);
      
          $image = wp_get_attachment_url($thumbnail_id);
      
          if ($image) {
      
              echo '<img src="' . esc_url($image) . '" alt="" />';
      
          }    }
      

      【讨论】:

        猜你喜欢
        • 2021-12-27
        • 2017-11-13
        • 2018-04-26
        • 2018-05-10
        • 2018-07-30
        • 1970-01-01
        • 2018-06-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多