admin管理员组文章数量:1130349
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!
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!
Share Improve this question asked Dec 18, 2018 at 21:27 Tim KingTim King 133 bronze badges 01 Answer
Reset to default 0The 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.
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!
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!
Share Improve this question asked Dec 18, 2018 at 21:27 Tim KingTim King 133 bronze badges 01 Answer
Reset to default 0The 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.
本文标签:
版权声明:本文标题:Having trouble with Template hierarchy. I Need to create a set of pages that drill down from states to specific locations 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749085751a2313746.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论