How to use custom slug and custom templates for custom taxonomy?

Question

I haven’t used taxonomies much, so not sure what the correct way to do this is.

I have a custom taxonomy called collections. I want an archive page for this that shows all the collections. I can manually make a page if needed. Ideally, these pages should only be accessible if a custom checkbox meta_key_value is 1 that I created for the taxonomy, but besides that, basic functionality does not work.

The slug for the taxonomy is: `’rewrite’ => array(‘slug’ => ‘folder/digital/collections’)

The url for the taxonomy archive should be https://example.com/folder/digital/collections/

Each collection url should resolve to https://example.com/folder/digital/collections/{slug} dynamically and only one template should work for all of them, such as taxonomy-collections-single, similar to a custom post type template.

A page already lives at https://example.com/folder/digital/ so not sure if this is causing a conflict.

I created a taxonomy-collections.php, however visiting https://example.com/folder/digital/collections/ produces a 404 error.

I tried following a post on here and created a taxonomy-collections-single.php and then added the following code to my functions.php:

function wpse_collections_template_include($original_template) {

    if (is_tax('collections')) {
        if(file_exists(get_template_directory(). '/taxonomy-collections-single.php')){
            return get_template_directory(). '/taxonomy-collections-single.php';
        }
    }
    return $original_template;
}
add_action('template_include', 'wpse_collections_template_include');

This appears to work for an individual collection that is assigned to a custom post type, but not for a collection that hasn’t been assign to a cpt yet. However, if I go to https://example.com/folder/digital/collections/ I still get a 404, even though I still have a file called taxonomy-collections.php.

I have been flushing the rewrite rules and re-saving permalinks before testing. I tried using 'hierarchical' => true, and 'hierarchical' => false, and 'has_archive' => true, or 'has_archive' => false, and an archive-collections.php just in case, as I wasn’t sure if this worked with taxonomies to begin with. Maybe I just need to create a new page called Collections and make it a child of the Digital page and use a query to load all the collections?

I would also like to know if I can restrict access to a single collection taxonomy page if the correct meta_key_value is not set, and instead redirect to a 404. The reason for this is that things need to be built prior to release, and the page should only be accessible once a specific checkbox has been enabled on a specific collection.

I would appreciate a point in the right direction as I’m stumped at this point as to why the archive won’t work and am not sure at all how to approach the meta key setup.

Thanks

0
liquidRock 4 weeks 2021-11-07T02:56:48-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse