简体   繁体   中英

Wordpress - How can I get_categories from additional custom post?

My goal is to get the Categories from the additional custom post I've registered in functions.php. How can I get its categories and echo it on my page? here are my code so far: from page:

 <?php
   $args = array(
   'post_type' => 'pdf',
   'orderby' => 'slug',
   'order' => 'ASC',
   'parent' => 0,
   'hide_empty' => false
   );
   $categories = get_categories($args);
   foreach( $categories as $category ){
   echo '<option><a class="ctg" href="'. get_category_link( $category->term_id ) .' ">' . $category->name . '</a></option>';
   }
  ?>      

from functions.php

  $args = array(
   'label' => 'category',
   'public' => true,
   'show_ui' => true,
   'show_in_nav_menus' => true,
   'show_admin_column' => true,
   'hierarchical' => true,
   'query_var' => true
  );
    register_taxonomy('pdf_cat','pdf',$args);
   

Categories on custom post types in wordpress are use the taxonomy functions. You can use get_terms to fetch the "categories" in this case.

Example

$categories = get_terms([
    'taxonomy' => 'pdf_cat',
    'hide_empty' => false,
]);

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