提问者:小点点

使用分类法图像插件为每个WooCommerce产品标签获取图像


我正在为Woocommerce开发一个插件,我使用Taxonomy图像插件为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>';

    }
}

感谢您的帮助。


共1个答案

匿名用户

而不是:

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

您应该使用:

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

现在,分类法图像插件将分类法图像存储在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>';
}

代码进入函数。活动子主题(或活动主题)的php文件。

测试和工作。