getting custom taxonomies terms of a post

后端 未结 1 2027
余生分开走
余生分开走 2021-01-17 01:05

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

     function create_product_taxonomies()
     {
        // Add new taxon         


        
相关标签:
1条回答
  • 2021-01-17 01:14

    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.

    0 讨论(0)
提交回复
热议问题