getting custom taxonomies terms of a post

China☆狼群 提交于 2019-12-01 13:15:40

问题


I am trying to get print names of custom taxonomy that i have created for products.

     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);

i have added data through the wordpress admin panel. Now i want to Display the names of the categories in product.php file.

    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 "";
}

}

it is just printing this arrayarrayarrayarrayarrayarray


回答1:


Ok bro you can use get_terms function for this purpose. Here is the example:

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         
     }
?>

I only give you an example. You can paste my code where you want.

Second Part

Now use the 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 this.

If you read taxonomy Template link then we go to next step.

Now you create a file taxonomy-product_categories.php in your theme root folder.

This create template for you taxonomy.

Now in this file here is the complete code:

<?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();
?>

Once again I told you that I give you only a code you can merge this code in your theme.

Hope this'll help you.



来源:https://stackoverflow.com/questions/36027820/getting-custom-taxonomies-terms-of-a-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!