2

I'm trying to set up a template that will group related entries by category, and display the category name and description for the first entry of each category set.

For example, this page will display an entry for a restaurant menu. The title and description for the menu are part of a single channel entry, followed by the items on the menu, which are all related entries from a products channel:

Menu Title

Menu Description

Appetizers

Item 1

Item 2

Item 3

Salads

Item 1

Item 2

Item 3

etc.

So my template code is like this:

{exp:channel:entries channel="menus"}
     <h1>{menu_title}</h1>
     {menu_description}
     {menu_items}
          Here's where I get stuck.
     {/menu_items}
{/exp:channel:entries}

I can readily display the category info with the categories tag pair:

{menu_items:categories}
     {category_name}
{/menu_items:categories}

What I can't seem to do is group them together, identify the first item etc.:

{menu_items:categories}
     {if count == 1}<h2>{category_name}</h3>{/if}
     <h3>{menu_items:product_name}</h3>
{/menu_items:categories}

I've tried several variations of the category_archive tag, as well as the Category Sorted Entries plugin, but to no avail.

I've also tried to back into it, by starting with the categories first, but it all get very convoluted, very quickly.

I feel like there has to be a more direct approach to sorting related records. Am I wrong?

Thanks,

ty

  • You mention that you want to group by category but then you mention that the items on the menu are all related entries. Relationships and categories are very different things so it could be useful to clarify which method you are looking to use. Also, I presume that by relationships you're referring to EE's native relationship field? – Ian Apr 13 '14 at 9:05
  • The related entries all have categories assigned to them. What I am trying to do is group them by category, within the related entries tag pair. – Ty Morton Apr 13 '14 at 13:52
0

I would look at using Playa's var_prefix parameter to avoid conflicts between your Playa variables and the outer tag pair.

Another potential issue I can see is that Playa sets the disable tag parameter to “categories|category_fields|member_data|pagination” by default, and you need to use the disable parameter to specify a smaller list of features to disable if you would like {categories} to be available.

Give this a try:

{exp:channel:entries channel="menus"}
     <h1>{menu_title}</h1>
     {menu_description}
     {menu_items var_prefix="m" disable="member_data|pagination"}
        {if "{m:count}" =="1"}
        {m:categories limit="1"}<h2>{category_name}</h2>{/m:categories}
        {/if}
        <h3>{m:product_name}</h3>
     {/menu_items}
{/exp:channel:entries}
|improve this answer|||||
  • I'm not using Playa. I'm using EE's native relationships field. That doesn't get me what I need, anyway, though. I think I'm going to have to try something different, like create a new relationship field for each category. – Ty Morton Apr 13 '14 at 13:59
0

I managed to achieve it using embeds. It's not ideal, but it works:

{exp:channel:categories category_group="1" style="linear"}
     {embed="menu/_menu_entry" catID="{category_id}" catURL="{category_url_title}" catNAME="{category_name}" catDESC="{category_description}"}
{/exp:channel:categories}

And then the embedded template searches the related entries by category:

{exp:channel:entries channel="menus" limit="1"}
     {menu_items category="{embed:catID}"}
          {if menu_items:count == 1}
        <div class="category-heading">
            <h3>{embed:catNAME}</h3>
            <div class="item-note"><p>{embed:catDESC}</p></div>
        </div>
    {/if}
        <div class="item">…

And then the items follow. The loop repeats for each category in the group, resulting in the desired format. It has the added advantage of being fully dynamic, so if the client adds, edits, or deletes categories, it still works.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.