x
 
Create New Page

Revision: Displaying Sub-categories and Grouping Them By Custom Category Field

Revision from: 14:19, 2 Feb 2008

The following code accomplishes much - sorting by county, then by city, with headings in the right places and showing once (not repeated) for every entry, then showing the listings of restaurants in alphabetical order by city. 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 {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="entries" style="linear"}

{
!-- 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 loopThis shows the restaurants by city. --}
     { embed
="entries/_listings4" my_cat="{current_category}" }

{
/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="entries" style="linear"}
{if ccf_county 
== "Orange"}
<h2>{category_name}</h2>
{ embed="entries/_listings4" my_cat="{current_category}" }
{
/if}
{
/exp:weblog:categories}


<h2 class="listings">Durham County</h2>

{exp:weblog:categories weblog="entries" style="linear"}
{if ccf_county 
== "Durham"}
<h2>{category_name}</h2>
{ embed="entries/_listings4" my_cat="{current_category}" }
{
/if}
{
/exp:weblog:categories}


<h2 class="listings">Forsyth County</h2>
{exp:weblog:categories weblog="entries" style="linear"}

{if ccf_county 
== "Forsyth"}
<h2>{category_name}</h2>
{ embed="entries/_listings4" my_cat="{current_category}" }
{
/if}
{
/exp:weblog:categories}

</div

Embedded template (called “entries/_listings4” above). This lists the restaurants.

{!-- separate out cities within a county and treat one at a time --}
     {exp
:weblog:entries weblog="entries" category="{embed:my_cat}" dynamic="off" orderby="title" sort="asc"}

{
!-- handle multiple categories being chosen to show only desired one --}
     {categories show
="{embed:my_cat}"}

{
!-- separate out restaurants by city --}
     {exp
:weblog:entries weblog="entries" limit="1" dynamic="off"}

{
!-- display restaurant name --}
     
<h3><u>{title}</u></h3>

{!-- display restaurant body text --}
     {cf_entry}

<p></p>
{/categories}
{
/exp:weblog:entries}

{
!-- handler of special situations that leave tags unclosed --}
     {if category_request}
          {
/exp:weblog:entries}
     {
/if} 

Small code is elegant code. This is another very elegant example of how embeds can allow tasks otherwise not possible, by filtering the data and isolating ID variables, then passing those in another variable to subroutines in an embedded template. Very beautiful!

Enjoy!

Terry

PS:
If you do use this little technique some way, please add a link here displaying where and how you did!

Category:Categories
Category:Tricks
Category:Organizing Data Display
Category:Embeds

Category:EE1