How to loop through custom taxonomy categories
-
I’m trying to figure out how to add my custom taxonomy categories to a template page and have them link to all custom post types of that category.
Here is what I have so far. I created a plugin to hold the functions creating the custom post type (contractor) and custom post taxonomy category (contractor_category). This is working as far as I have a template page(page-contractors-full-width.php) that lists all of the contractors. At the top of the page I have a loop that lists all of the custom categories (contractor_category). The issue is the links are going to a 404 page. I can’t figure out what the slug should be for each custom category. I have tried /contractor/contractor_category and /contractor_category
I tried creating archive-contractor.php and archive-contractor_category.php and taxonomy-contractor_category.php just to see if any of those would work but they didn’t work. I’ve pureed reading through the wordpress docs on this but it’s a bit over my head. I’m better looking at examples lol..
Here is my plugin code where create the custom post type and custom taxonomy
add_action( 'init', 'create_contractor_category_tax' ); function create_contractor_category_tax() { register_taxonomy( 'contractor_category', 'contractor', array( 'label' => __( 'Contractor Category' ), 'rewrite' => array( 'slug' => 'contractor_category' ), 'hierarchical' => true, ) ); } function nari_post_type_contractor() { $supports = array( 'title', // post title 'editor', // post content 'author', // post author 'thumbnail', // featured images 'excerpt', // post excerpt 'custom-fields', // custom fields 'comments', // post comments 'revisions', // post revisions 'post-formats', // post formats ); $labels = array( 'name' => _x('Contractors', 'plural'), 'singular_name' => _x('Contractor', 'singular'), 'menu_name' => _x('Contractors', 'admin menu'), 'name_admin_bar' => _x('Contractors', 'admin bar'), 'add_new' => _x('Add New', 'add new'), 'add_new_item' => __('Add New Contractor'), 'new_item' => __('New Contractor'), 'edit_item' => __('Edit Contractor'), 'view_item' => __('View Contractor'), 'all_items' => __('All Contractors'), 'search_items' => __('Search Contractors'), 'not_found' => __('No Contractors found.'), ); $args = array( 'supports' => $supports, 'labels' => $labels, 'public' => true, 'query_var' => true, 'rewrite' => array('slug' => 'contractor'), 'has_archive' => true, 'hierarchical' => false, ); register_post_type('contractor', $args); } add_action('init', 'nari_post_type_contractor');
Here is the code where I loop through and get the custom taxonomy links
// Get the taxonomy's terms $terms = get_terms( array( 'taxonomy' => 'contractor_category', 'hide_empty' => true, ) ); // Check if any term exists if ( ! empty( $terms ) && is_array( $terms ) ) { // add links for each category foreach ( $terms as $term ) { ?> <a class="btn btn-default" href="<?php echo esc_url( get_term_link( $term ) ) ?>"> <?php echo $term->name; ?> </a><?php } }
Any help would be greatly appreciated ??
- The topic ‘How to loop through custom taxonomy categories’ is closed to new replies.