wordpress 如何在Woocommerce中从post-meta获取自定义分类?

krugob8w  于 5个月前  发布在  WordPress
关注(0)|答案(1)|浏览(73)

我有一些问题的尝试显示从自定义分类在WooCommerce的术语。
目前,我已经使用了WooCommerce产品类别,所以我创建了一个名为sale_type的自定义分类法

function add_custom_taxonomies() {
  // Add new "Sale Type" taxonomy to Posts
  register_taxonomy('sale_type', 'product', array(
    // Hierarchical taxonomy (like categories)
    'hierarchical' => true,
    // This array of options controls the labels displayed in the WordPress Admin UI
    'labels' => array(
      'name' => _x( 'Sale Type', 'taxonomy general name' ),
      'singular_name' => _x( 'Sale Type', 'taxonomy singular name' ),
      'search_items' =>  __( 'Search Sale Type' ),
      'all_items' => __( 'All Sale Type' ),
      'parent_item' => __( 'Parent Sale Type' ),
      'parent_item_colon' => __( 'Parent Sale Type:' ),
      'edit_item' => __( 'Edit Sale Type' ),
      'update_item' => __( 'Update Sale Type' ),
      'add_new_item' => __( 'Add New Sale Type' ),
      'new_item_name' => __( 'New Sale Type Name' ),
      'menu_name' => __( 'Sale Type' ),
    ),
    // Control the slugs used for this taxonomy
    'rewrite' => array(
      'slug' => 'sale-type', // This controls the base slug that will display before each term
      'with_front' => false, // Don't display the category base before "/locations/"
      'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
    ),
  ));
}
add_action( 'init', 'add_custom_taxonomies', 0 );

字符串
然后,在我尝试在单个产品页面上显示自定义分类sale_type的术语名称失败时,我使用了wp_get_post_terms函数,类似于我对'product_cat'的处理方式。
下面是一个示例:

<span class="badge">
    <?php
    $terms = wp_get_post_terms(get_the_id(), 'sale_type');
    $term = reset($terms);
    echo $term->name;
    ?>
</span>

<span class="badge"><?php $terms=wp_get_post_terms(get_the_id(),'product_cat');$term=reset($terms);echo $term->name; ?></span>


输出是什么。所以,首先,我做了显而易见的,并检查了'销售_type'自定义分类元盒,并验证了一个术语被选中。然后,我试图使用get_the_terms而不是wp_get_post_terms -然后还做了一个var转储,它的输出= array(0){ }
在这个产品的post Meta中,我可以看到所有不同的自定义字段,但是在这个输出中我没有看到“销售_type”分类。
我以前从来没有真正使用过自定义分类法,所以我现在很好奇,“销售_type”是否会以不同的方式存储在post meta中,或者根本不存储在post Meta中。那么,有没有一种不同的方法可以直接获取自定义分类法术语?

vfwfrxfs

vfwfrxfs1#

我已经尝试了你的WooCommerce产品自定义分类,它的工作原理.现在我已经做了一些小的变化,你的显示代码.尝试以下:

$term_names = wp_get_post_terms( get_the_ID(), 'sale_type', array('fields' => 'names') );
if ( ! empty($term_names) ) {
    printf('<span class="badge">%s</span>', implode(' ', $term_names));
}

字符串
如果在产品上设置了术语名称,代码将正确显示术语名称...

  • 我已经将get_the_id()更改为get_the_ID()
  • 我将array('fields' => 'names')作为参数添加到wp_get_post_terms()中,以直接获取术语名称数组,
  • 由于一个产品可以有多个术语名称,我使用implode()而不是reset()
  • 最后,我使用一个IF语句来避免在没有为产品设置术语名称时显示。

为了进行显示测试,我对单个产品页面 (在Meta部分) 使用了以下内容:

add_action( 'woocommerce_product_meta_end', 'single_pproduct_display_sale_type_test', 10 );
function single_pproduct_display_sale_type_test(){
    global $product, $post;
  
    $term_names = wp_get_post_terms( get_the_ID(), 'sale_type', array('fields' => 'names') );
    if ( ! empty($term_names) ) {
        printf('<p class="sale_type">%s: <strong>%s</strong></p>', get_taxonomy('sale_type')->labels->name, implode(' ', $term_names));
    }
}


代码放在你的子主题的functions.php文件中(或插件中)。测试和工作。


的数据

相关问题