代码之家  ›  专栏  ›  技术社区  ›  Eduardo Relax

如何获取WooCommerce产品类别url以在HTML元标记中显示?

  •  0
  • Eduardo Relax  · 技术社区  · 9 月前

    下面,当它是一个产品页面时,我可以显示产品永久链接,但我想对产品类别存档页面也这样做。

    这是我的代码:

    <meta name="twitter:url" content="<?php
    if( is_product_category() ){
       $cat_link = get_category_link($category->cat_ID);
       echo $category; // category link
    } else
    the_permalink(); ?>" />
    

    我得到的是产品类别档案的空洞回声。

    如何获取WooCommerce产品类别url以在HTML元标记中显示?

    2 回复  |  直到 9 月前
        1
  •  1
  •   LoicTheAztec    9 月前

    WooCommerce产品类别 (自定义分类) 使用WordPress get_term_link() 功能如下:

    <meta name="twitter:url" content="<?php
    if( is_product_category() ){
        $term_link = get_term_link(get_queried_object_id(), 'product_cat');
        if ( ! is_wp_error( $term_link ) ) {
            echo $term_link;
        }
    } else {
        the_permalink(); 
    } ?>" />
    

    它应该工作。

        2
  •  0
  •   Oleksandr Chechota    9 月前

    也许你应该这样写:

    $cat_link=get_category_link($category->cat_ID); echo$cat_link ; // 类别链接