Having trouble with Template hierarchy. I Need to create a set of pages that drill down from states to specific locations

I am creating an interactive set of pages that needs to be very easy to navigate, as the audience skews older. Walking through the flow The user will start at a page, that lists all of the (US) states. The user clicks on a state and it takes them to a state page, and lists all of the cities that have work sites within that state. When the user clicks on a city it takes them to a city page, and provides a list of the work sites within that city. Each individual step in this workflow needs to have a corresponding template, allowing me to show different state/city/site information.

Where I am right now:

I've created a custom post type, along with taxonomies for city and state. I'm assuming that each site will be a post and I will be able to group them with the taxonomies.

Where I need help:

The organization of this is confusing me. I'm not sure I set the structure up correctly for the flow that I listed above. Any help explaining the correct way to approach setting up post types/taxonomies/templates for the above flow would be greatly appreciated!

Answers 1

  • The way I would organise this (and I have done this in the past) would be to have a post type for Work Sites, and a hierarchical taxonomy for Locations.

    Then what you would do is add States as the top-level of the Locations taxonomy, and then add Cities as children of the State locations. Then you can assign a Work Site to a City and State.

    Normally when you do this, if you attempt to view a State, rather than seeing a list of Cities you'll see a list of Work Sites in that State. Which is not what you want. So what we need to do is modify the archive template for Locations to list subcategories instead of work sites.

    So you'll need to create a taxonomy-location.phpto use as the template when viewing States and Cities. Normally this template will have something like this in it:

    <?php while ( have_posts() ) : the_post(); ?>
        // Work Site template here.
    <?php endwhile; ?>
    

    This will list the Work sites in that Location. We need to change this to only list Work Sites if there are no subcategories for the current Location, otherwise list the subcategories:

    // Get subcategories of current location.
    $current_location = get_queried_object();
    $sub_locations    = get_terms( [
        'taxonomy' => $current_location->taxonomy,
        'parent'   => $current_location->term_id,
    ] );
    
    // If there are subcategories for the current Location...
    if ( ! empty( $sub_locations ) ) :
        // ...loop through and display them.
        foreach( $sub_locations as $location ) :
            // Location template here.
            echo $location->name; // Location name;
            echo $location->description; // Location description.
            echo get_term_link( $location ); // Location URL.
        endforeach;
    // Otherwise...
    else : 
        // ...list Work Sites for the current location.
        while ( have_posts() ) : the_post();
            // Work Site template here.
        endwhile;
    endif;
    

    This method is flexible and will allow you have as many levels of Locations that you want, and only the lowest level will display the Work Sites.


Related Questions