获取帖子的自定义分类法条款 [英] getting custom taxonomies terms of a post

查看:69
本文介绍了获取帖子的自定义分类法条款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

}

它只是打印此arrayarrayarrayarrayarrayarrayarray

it is just printing this arrayarrayarrayarrayarrayarray

推荐答案

好了,您可以使用 get_terms 函数。例如:

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


第一部分



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


第二部分

现在使用 WordPress分类模板,当用户单击您的类别之一时,下一页将显示单击类别的所有相关产品,并且您还必须阅读此内容。

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.

现在您在自己的文件中创建文件 taxonomy-product_categories.php 主题根文件夹。

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

此分类模板为您创建。

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

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.

这篇关于获取帖子的自定义分类法条款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆