获取 post 的自定义分类术语

getting custom taxonomies terms of a post

我正在尝试获取我为产品创建的自定义分类法的打印名称。

     function create_product_taxonomies()
     {
        // Add new taxonomy, make it hierarchical (like categories)
         $labels = array(
                   'name' => _x('product_categories', 'taxonomy general name'),
                   'singular_name' => _x('Product', 'taxonomy singular name'),
                   'search_items' => __('Search Product Category'),
                   'all_items' => __('All Product Categorie(s)'),
                   'parent_item' => __('Parent Product Category'),
                   'parent_item_colon' => __('Parent Product Category:'),
                   'edit_item' => __('Edit Product Category'),
                   'update_item' => __('Update Product Category'),
                  'add_new_item' => __('Add New'),
                  'new_item_name' => __('New Product Name'),
                  'menu_name' => __('Product Categories'),

                  );

       $args = array(
               'hierarchical' => true,
               'labels' => $labels,
               'show_ui' => true,
               'show_admin_column' => true,
               'query_var' => true,
              'rewrite' => array('slug' => 'product_categories', 'with_front' => true));

      register_taxonomy('product_categories', array('products'), $args);

我已经通过 wordpress 管理面板添加了数据。现在我想显示 product.php 文件中的类别名称。

    function getLatestProducts()
    { 
       $args = array(
                'post_status' => 'publish',
                'post_type' => 'products', 
                'posts_per_page' => 12, 
               'order' => 'ASC'
             );
      $result = '<div class="col-sm-3">';
      $loop = new WP_Query($args);
      $i=0;
      while ($loop->have_posts()) 
      {
         $loop->the_post();
         $clink=get_permalink($post->ID);
         $desc=get_the_excerpt();
         $categories = get_terms( 'product_categories');
         $desc = strip_tags(str_replace(array("<p>", "</p>"), "", $desc)); 
         $the_imgurl = get_post_custom_values('_cus_n__image');
         $theimage=$the_imgurl[0];
         $the_locurl = get_post_custom_values('_cus_n__location');
         $theloc=$the_locurl[0];
         echo $categories;

         $result .='<div class="product-warp">';
         $result .='<div class="product"> <a href="#"><img src="/wp-content/themes/cake/images/pro1.jpg" title="" alt=""></a> </div>';
         $result .='<div class="product-name">';
         $result .='<h5><a href="#">'.$categories.'</a></h5>';
         $result .='</div>';
         $result .='</div>';
         $i++;

     }
    $result .= '</div>';
    if($i > 0){
      return $result;
    } else {
       return "";
}

}

它只是打印这个数组arrayarrayarrayarrayarray

好的兄弟,你可以使用 get_terms 功能来达到这个目的。这是示例:

First Part

<?php
     $args = array(
                 'orderby' => 'name'
             );
     $terms = get_terms('product_categories', $args);

     foreach($terms as $term) {
?>
         <a href="<?php echo get_term_link($term->slug, 'product_categories') ?>">
             <?php echo $term->name; ?>
         </a>
<?php         
     }
?>

我只是举个例子。你可以把我的代码粘贴到你想要的地方。

Second Part

现在使用WordPress Taxonomy Template for that when user click on one of your category and next page show all the related products of clicked category and also you must read这个。

如果您阅读 taxonomy Template link 那么我们进入下一步。

现在您在主题根文件夹中创建一个文件 taxonomy-product_categories.php

这为您的分类创建模板。

现在在这个文件中是完整的代码:

<?php
    get_header();

    $slug = get_queried_object()->slug; // get clicked category slug
    $name = get_queried_object()->name; // get clicked category name

    $tax_post_args = array(
        'post_type' => 'products', // your post type
        'posts_per_page' => 999,
        'orderby' => 'id',
        'order' => 'ASC',
        'tax_query' => array(
            array(
                'taxonomy' => 'product_categories', // your taxonomy
                'field' => 'slug',
                'terms' => $slug
            )
        )
    );
    $tax_post_qry = new WP_Query($tax_post_args);

    if($tax_post_qry->have_posts()) :
       while($tax_post_qry->have_posts()) :
          $tax_post_qry->the_post();

          the_title();

          the_content();

       endwhile;
    endif;

    get_footer();
?>

我再一次告诉你,我只给你一个代码,你可以将这个代码合并到你的主题中。

希望对您有所帮助。