I am finally satisfied - We’ve accomplished much - sorting by county, then by city, with headings in the right places and not repeated for every entry. Now I will sleep.
This has one custom category field, called {ccf_county} which is a required field when the customer wishes to add a new category (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 {categories show=”{embed:my_cat}”} parameter to handle 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! I figure the client will select both cities if the restaurant exists in two (or more) and so had to cover for that.
The variable {current_category} is a “Fresh Variable” made with that module. It has these settings:
current_category = {exp:weblog:entries limit=“1” dynamic=“off”}{categories}{category_id}{/categories}{/exp:weblog:entries}
These {current_category} Fresh variables could be replaced in the following code by simply entering in longhand in their place:
{exp:weblog:entries limit="1" dynamic="off"}{categories}{category_id}{/categories}{/exp:weblog:entries}
but this looks much, much neater!
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)
{!-- 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}
Small code is elegant code. This is another very elegant example of how embeds can allow tasks not able to be accomplished except by filtering data and isolating ID variables, and then passing those to subroutines in another template. Very beautiful!
Enjoy!
Terry
[Edit:] I’ve created a wiki article out of this post, as I think this technique could enjoy many applications. If you do use it, please add a link there displaying where and how you did!
[Edit2:] I’ve updated this post to match the Wiki entry mentioned above.