1
votes

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

1

1 Answers

0
votes

I'm trying to understand the problem and from a wild guess I would say that you're messing up your pathing somehow. Upon calling <?php echo esc_url( get_term_link( $term ) ) ?> the url should work, so my guess is that you've touch the url pathing in your admin consol.

You've got to understand the difference between custom taxonomy and custom post type.

This path would go to a archive page: /contractor (archive-contractor.php or if it doesn't exist archive.php)

This path would go to a taxonomy page: /contractor/contractor_category (taxonomy-contractor_category.php or if it doesn't exist taxonomy.php).

If you dont have an archive file nor a taxonomy file with a valid loop, you will get a 404 error if the page exist and if not you will be re-directed to the home page.

Your code is fine. From what you're giving me the only fix to your problem I see currently would be to create a blank slate theme with the minimum required (no plugin everything in the function.php file) and start from scratch by removing all the exes code you have just to get a valid demo. Try to get a clear picture a you will find your problem.