The following code accomplishes much - sorting by county, then by city, with city headings in the right places and showing once (not repeated) for every restaurant entry, then showing the listings of restaurants in alphabetical order by city under each city heading. Read the commented code and see if you follow its logic. It uses categories which can be added by the client to organize the entries, in this case being cities in one of four counties. The entries themselves are restaurants in each city, as an example of this code’s possible use.
This category_group has one custom category field, called {ccf_county} which is a required field when the customer wishes to add a new category (in our case, to add a city name).
There are only four counties covered, so testing in this case can be hard-wired into the code by testing for the actual name. Should probably include a check for upper/lower case first letter.
The city is the actual category name, so it needs only to be isolated by county. Then keeping the city establishments all together and not merely presented in the order the entries (restaurants) were added is needed. And to cap things off, the city name now sits on top of all its restaurants.
See the Sample page (adapted from content on the actual website where this will be used) to see this short bit in action.
An additional bonus is using the {category=”{embed:my_cat}”} parameter handles cases where there are two city/county locations of the same restaurant, keeping the two in their respective cities and counties and not showing atop one another twice!
Main snippet:
<div id="content">
<h1>Restaurants</h1>
<h2 class="listings">Butner County</h2>
{!-- sweep through categories to sift desired ones out --}
{exp:weblog:categories weblog="restaurants" style="linear" show_empty="no"}
{!-- screen out county name from category custom field --}
{if ccf_county == "Butner"}
{!-- display the city name --}
<h2>{category_name}</h2>
{!-- workhorse -- nested category/entries sifting loop. This shows the restaurants by city. --}
{ embed="entries/_listings4" my_cat="{category_id}" }
{/if}
{/exp:weblog:categories}
{!-- the rest share the same code with county name change hardwired --}
<h2 class="listings">Orange County</h2>
{exp:weblog:categories weblog="restaurants" style="linear" show_empty="no"}
{if ccf_county == "Orange"}
<h2>{category_name}</h2>
{ embed="entries/_listings4" my_cat="{category_id}" }
{/if}
{/exp:weblog:categories}
<h2 class="listings">Durham County</h2>
{exp:weblog:categories weblog="restaurants" style="linear" show_empty="no"}
{if ccf_county == "Durham"}
<h2>{category_name}</h2>
{ embed="entries/_listings4" my_cat="{category_id}" }
{/if}
{/exp:weblog:categories}
<h2 class="listings">Forsyth County</h2>
{exp:weblog:categories weblog="restaurants" style="linear" show_empty="no"}
{if ccf_county == "Forsyth"}
<h2>{category_name}</h2>
{ embed="entries/_listings4" my_cat="{category_id}" }
{/if}
{/exp:weblog:categories}
</div>
Embedded template (called “entries/_listings4” above). This lists the restaurants.
{!-- treat cities within a county one at a time --}
{exp:weblog:entries weblog="restaurants" category="{embed:my_cat}" dynamic="off" orderby="title" sort="asc"}
{!-- display restaurant name --}
<h3><u>{title}</u></h3>
{!-- display restaurant body text --}
{cf_entry}
<p></p>
{/exp:weblog:entries}
Enjoy!
Terry
PS:
If you do use this little technique some way, please add a link here displaying where and how you did!
PPS: Studying another technique allowing use of nested categories (parent/child) which may allow automating styling by using the weblog:category_archive tags instead of the weblog:categories tag.
Category:Categories
Category:Tricks
Category:Organizing Data Display
Category:Embeds
