Home page for a category / taxonomy

I don’t know why WP themes do not support taxonomy homepages. I mean there should be a separate template for example.com/location/ (this may show a list of available taxonomy terms etc.) in addition to example.com/location/newyork/ where location is a taxonomy.

What is the best way to have a taxonomy home? A page with slug=location and custom template?

Related posts

2 comments

  1. The /location page doesn’t exist because WordPress doesn’t just make a page based off your URL structure.
    You can create a “Page” called “location” if you need to show content there.

    and if you want to list the available terms you have in the location taxonomy you can create a custom page template ex:

    <?php
    /**
     * Template Name: Locations archive
     *
     * @package WordPress
     * @subpackage Twenty_Twelve
     * @since Twenty Twelve 1.0
     */
    
    get_header(); ?>
    
        <div id="primary">
            <div id="content" role="main">
    
                <?php while ( have_posts() ) : the_post(); ?>
                    <?php get_template_part( 'content', 'page' ); ?>
                <?php endwhile; // end of the loop. 
                $taxonomy = 'locations';
                $terms = get_terms($taxonomy,array(
                    'orderby'       => 'name', 
                    'order'         => 'ASC',
                    'hide_empty'    => true    
                ));
    
                if ( count($terms) > 0 ){
                    echo "<ul>";
                    foreach ( $terms as $term ) {
                        echo '<li><a href="'.get_term_link($term->slug, $taxonomy).'">'.$term->name.'</a></li>';
                        //do other stuff for each term
                    }
                    echo "</ul>";
                }
                ?>
    
            </div><!-- #content -->
        </div><!-- #primary -->
    
    <?php get_footer(); ?>
    

    This will list all terms in locations taxonomy with links to each term’s archive.

  2. Taxonomies actually do have archive pages like that, in the same way that categories do. You just have to be sure that when you register the taxonomy, that you do so with rewrites enabled and a slug. For example:

    // Control the slugs used for this taxonomy
        'rewrite' => array(
            'slug' => 'locations', // This controls the base slug that will display before each term
            'with_front' => false, // Don't display the category base before "/locations/"
            'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
        ),
    

    From Smashing Magazine

    If they don’t immediately show up, be sure to visit settings > permalinks to reset the rewrites.

    To create a template specific to a particular taxonomy, in your case location, copy archive.php and rename the copy taxonomy-location.php. This will give you a template specific to that taxonomy. If you want to create a template specific to an individual term, rename it taxonomy-location-newyork.php.

Comments are closed.