【问题标题】:Show categories under product in shop Woocommerce - Keeps displaying ''Array''在商店 Woocommerce 中的产品下显示类别 - 继续显示“数组”
【发布时间】:2020-02-25 21:26:55
【问题描述】:

所以我想弄清楚如何在这个网站上显示产品下的类别:http://lauragdiaz.com/the-list/ 产品是品牌(Alltruiest、BKIND 等)

我尝试用这篇文章修复代码:Get Order items and WC_Order_Item_Product in WooCommerce 3 但是每次我这样做时,它都会在我的代码中显示一个致命错误。

我不确定我做错了什么。 我现在拥有的显示“数组”的代码是这个:

function skyverge_shop_display_skus() {

    global $product;

    if ( $product->get_category_ids() ) {
        echo '<div class="product-meta">Catégorie: ' . $product->get_category_ids() . '</div>';
    }

}


add_action( 'woocommerce_after_shop_loop_item', 'skyverge_shop_display_skus', 9 );

我应该把 get_category_ids 放在哪里?以及如何让信息显示?

谢谢!

【问题讨论】:

    标签: javascript php wordpress woocommerce


    【解决方案1】:

    这样的事情会奏效。因为我需要显示标签/类别。这也允许他们链接到术语链接,以便他们可以通过这些链接加载相关产品。

    add_action('woocommerce_shop_loop_item_title', 'add_tags_and_category', 15);
    function add_tags_and_category() { ?>
        <div class="tags">
        <?php $product_tags = get_the_terms( get_the_ID(), 'product_tag') ;
        if( $product_tags && ! is_wp_error( $product_tags ) ) :
            foreach( $product_tags as $tag) : ?>
                <a href="<?php echo get_term_link( $tag->slug, 'product_tag'); ?>" rel="tag" class="btn btn-primary btn-sm mb-4"><?php echo $tag->name; ?></a>
            <?php endforeach;
        endif;
        $product_terms = get_the_terms( get_the_ID(), 'product_cat');
        if( $product_terms && ! is_wp_error( $product_terms ) ) :
            foreach( $product_terms as $term) : ?>
                <a href="<?php echo get_term_link( $term->slug, 'product_cat'); ?>" rel="tag" class="btn btn-primary btn-sm mb-4"><?php echo $term->name; ?></a>
            <?php endforeach;
        endif; ?>
        </div>
    <?php }
    

    更改 HTML 以满足您的需要。如果您只需要产品类别。只需删除第一个 if

    【讨论】:

    • 非常感谢它的工作!有没有办法不让它们可点击(类别/标签)?我试图删除 a=href 但它破坏了代码所以我不确定......
    • 这应该可以正常工作。您可能希望向跨度或其他内容添加一个类,以便您可以在它们之间留出空格。 php $product_terms = get_the_terms( get_the_ID(), 'product_cat'); if( $product_terms &amp;&amp; ! is_wp_error( $product_terms ) ) : foreach( $product_terms as $term) : ?&gt; &lt;span&gt;&lt;?php echo $term-&gt;name; ?&gt;&lt;/span&gt; &lt;?php endforeach; endif; ?&gt;
    • 非常感谢!真的很感激:)
    • 不客气。如果您觉得我的回答完全回答了您的问题。请将其标记为已回答。
    猜你喜欢
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2021-12-12
    相关资源
    最近更新 更多