简体   繁体   中英

Display taxonomy from custom post type

I currently have a custom post type of "Products".

当前的自定义帖子类型。

As you can see, I now have External and Internal products, which was created by this code:

    add_action( 'init', 'create_product__cat_external' );
function create_product__cat_external() {
    register_taxonomy(
        'ExternalProducts',
        'products',
        array(
            'label' => __( 'External Products' ),
            'rewrite' => array( 'slug' => 'externalproducts' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'create_product__cat_internal' );

function create_product__cat_internal() {
    register_taxonomy(
        'InternalProducts',
        'products',
        array(
            'label' => __( 'Internal Products' ),
            'rewrite' => array( 'slug' => 'internalproducts' ),
            'hierarchical' => true,
        )
    );
}

What I am trying to achieve, is a page which can display ONLY the categories inside External Products.

I have this snippet of code, which displays BOTH external and Internal:

<?php
              $customPostTaxonomies = get_object_taxonomies('products');

              if(count($customPostTaxonomies) > 0)
              {
                   foreach($customPostTaxonomies as $tax)
                   {
                         $args = array(
                            'orderby' => 'name',
                            'show_count' => 0,
                            'pad_counts' => 0,
                            'hierarchical' => 1,
                            'taxonomy' => $tax,
                            'title_li' => ''
                            );

                         wp_list_categories( $args );
                   }
             }
             ?>

Any help would be great. Cheers.

UPDATE:

I currently have the name of the category, and description outputting, but the link does not link to display the posts inside this category.

Code below:

<?php
             $taxonomy = 'ExternalProducts';
             $queried_term = get_query_var($taxonomy);
             $terms = get_terms($taxonomy, 'slug='.$queried_term);
             if ($terms) {
              echo '<ul>';
              foreach($terms as $term) {
   // The $term is an object, so we don't need to specify the $taxonomy.
                $term_link = get_term_link( $term );

// If there was an error, continue to the next term.
                if ( is_wp_error( $term_link ) ) {
                  continue;
            }

// We successfully got a link. Print it out.
            echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
            echo $term->description;
      }
      echo '</ul>';
}
?>

You need to use the function " get_terms ".Here is the brief code. Suppose your taxonomy name = custom_taxonomy

$catlsit = get_terms('custom_taxonomy', array( 'orderby' => 'count', 'hide_empty' => 0 ) ); print_r($catlsit); You will get the category list which is created in taxonomy :custom_taxonomy. if you need any other then please write down. Thanks,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM