Custom template for archive of a custom taxonomy

I’ve made a custom taxonomy named activities, containing items named local, member, and national:

<?php
function add_custom_taxonomies() {
    register_taxonomy('Activities', 'post', array(
        'has_archive' => true,
        'hierarchical' => true,
        'labels' => array(
            // labels goes here
        ),
        'rewrite' => array(
            'slug' => 'activities',
            'with_front' => true, 
            'hierarchical' => true
        ),
    ));
}
add_action( 'init', 'add_custom_taxonomies, 0 );

And I’ve created each taxonomy item a page, which are taxonomy-activities-local.php, taxonomy-activities-member.php, and taxonomy-activities-national.php.

Accessing mydomain.com/activities/local/ is just fine. But accessing mydomain.com/activities/ itself redirects to a 404 page. I understand fully that this normally shouldn’t work. It is just like accessing mydomain.com/tag/, which also redirects to a 404 page.

Is there a way to use a template PHP file that I made? Hence, accessing mydomain.com/activities/ uses that PHP file instead of the 404 page without using any plugins?

1 Answer
1

I think the best way to do it is to create a custom template page and get the list of terms (or whatever you need) on that template.

template-activities-taxonomy.php

<?php
/* Template Name: Activities Taxonomy */

get_header(); ?>

<?
$terms = get_terms( 'activities' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ): ?>
    <ul>
    <?php foreach ( $terms as $term ): ?>
        <li><a href="https://wordpress.stackexchange.com/questions/273555/<?php echo esc_url( get_term_link( $term ) ); ?>"><?php echo $term->name; ?></a></li>    
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

<?php get_footer(); ?>

And then just create a Page with the url you want (/activities) that uses that template.

Leave a Comment