【问题标题】:Get an image for each Woocommerce product tag using Taxonomy Images plugin使用分类图像插件为每个 Woocommerce 产品标签获取图像
【发布时间】:2018-06-30 06:20:01
【问题描述】:

我正在为 Woocommerce 开发一个插件,我使用 Taxonomy Images 插件为 Woocommerce 产品标签自定义分类添加图像功能。

现在我试图在一个函数中显示每个产品的产品标签名称和他的对应图像。但可能是我失败了,因为我看到了所有标签,而不仅仅是特定产品的标签。

为了获得产品 ID,我尝试使用 global $product;,但它不起作用。

这是我的代码:

function woo_idoneo_tab_content() {

    $id = get_the_ID();

    $terms = apply_filters( 'taxonomy-images-get-terms', '', array('taxonomy' => 'product_tag', 'id' => $id, ) );

        if ( ! empty( $terms ) ) {

            print '<div class="container">';

            foreach ( (array) $terms as $term) {

            $url = get_term_link ($term->slug, 'product_tag' , $id);


            print '<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">';
            print '<div class="card">';
            print '<div class="card-img-top"><a href="'.$url.'">' . wp_get_attachment_image( $term->image_id, 'medium' ) . '</a></div>';
            print '<div class="card-body">';        
            print "<h5 class='card-title'><a class='btn btn-primary' href='{$url}'>{$term->name}</a></h5>";

            }

            print '</div>';
            print '</div>';
            print '</div>';
        }
            print '</div>';

    }
}

感谢任何帮助。

【问题讨论】:

  • 您需要将代码直接放在问题上,而不仅仅是指向 pastebin 的链接。这是本站的规定。谢谢。

标签: php wordpress woocommerce product custom-taxonomy


【解决方案1】:

代替:

$terms = apply_filters( 'taxonomy-images-get-terms', '', array('taxonomy' => 'product_tag', 'id' => $id, ) );

你应该使用:

$terms = wp_get_post_terms( get_the_ID(), 'product_tag' );

现在Taxonomy Images 插件将分类图像存储在wp_otion 表中。要获取该数据,您将使用:

$custom_taxonomy_images = get_option( 'taxonomy_image_plugin' ); // Array of term Ids / Image Ids pairs 

所以你现在重新访问的代码是:

function woo_idoneo_tab_content() {
    // Check that plugin is active, if not we exit.
    if( ! is_plugin_active( 'taxonomy-images/taxonomy-images.php' ) ) return;

    $taxonomy = 'product_tag';
    $post_id = get_the_ID();
    $terms = wp_get_post_terms( $post_id, $taxonomy ); // Terms for this post
    $custom_taxonomy_images = get_option( 'taxonomy_image_plugin' ); // Plugin images data

    if ( empty( $terms ) ) return; // If no terms found, we exit.

    ## -- HTML Output below -- ##

    echo '<div class="container">';
    // Loop through each term in this post for the defined taxonomy
    foreach ( $terms as $term ) {
        $attachment_id = $custom_taxonomy_images[intval($term->term_id)]; // Get image Attachement ID
        $image = wp_get_attachment_image( $attachment_id, 'medium' ); // Get image to be displayed
        $url = get_term_link( $term->term_id, $taxonomy ); // Get term URL
        ?>
        <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
            <div class="card">
                <div class="card-img-top">
                    <a href="<?php echo $url; ?>"><?php echo $image; ?></a>
                    </div>
                <div class="card-body">
                    <h5 class="card-title">
                        <a class="btn btn-primary" href="<?php echo $url; ?>"><?php echo $term->name; ?></a>
                    </h5>
                </div>
            </div>
        </div>
        <?php
    }
    echo '</div>';
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。

经过测试并且有效。

【讨论】:

  • 谢谢,一切正常。一个问题,intval参数是什么?
  • @user3421040 [inval(http://php.net/manual/en/function.intval.php) php 函数]() 允许获取变量的整数值……如果这个答案回答了您的问题,请don't forget to accept the answer,谢谢。
猜你喜欢
  • 1970-01-01
  • 2018-05-10
  • 2018-10-21
  • 1970-01-01
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
相关资源
最近更新 更多