1

I need to restructure the channel entries (I'm trying to group them by one of the channel fields). For example, say I have a list of fruits and each fruit had a color. I'm trying to group these fruits in an associative array by color, like:

$fruits['yellow'] = array('fruit_name' => 'banana' ...

Once I have this restructured data, I then need to loop over it and output certain mark up.

So far, what I have done is to set Allow PHP? to Yes and declared an array, built that array within the entries tag pair, and now going to loop through that array and output the markup I need.

This seems messy. Is there a "cleaner" approach?

UPDATE

This is how I want to output the data, which I think would be impossible to achieve with an orderby:

Yellow
 -- Banana
 -- Lemon
 -- Papaya

Red
 -- Apple
 -- Strawberry
 -- Cherry

So, I need to loop over the colors, then loop over the fruits within each color. It's a nested array that I need. Using orderby does not give me a nested array.

5 Answers 5

3

Perhaps you could use the orderby parameter on your custom field, and use PHP to monitor that field, and, each time it changes, perform your custom display behavior?

Edit 1: Setting up Categories

It turns out that you should probably be using categories to display your entries. Using your example above, you should setup a Category Group; there are many strategies for managing categories/sub categories; we'll assume you just need a single Category Group called "Colors."

In any event, create the "Colors" category group (Admin > Channel Administration > Categories > [Create a New Category Group] > Name it "Colors" > Add/Edit Categories > etc), then, tie it to your channel (Admin > Channel Administration > Channels > Edit Group Assignments > [Category Groups]).

After jumping through those hoops, you'll be able to specify which colors are associated with each entry when editing them.

Once categories are in established, displaying them in a template is straight forward:

{exp:channel:categories style="linear"}
  <h3>{category_name}</h3>
  {exp:channel:entries category="{category_id}"}
    <p><a href="{page_url}">{title}</a></p>
  {/exp:channel:entries}
{/exp:channel:categories}
11
  • 1
    I really think I need a nested array. Question updated. Dec 3, 2012 at 20:41
  • 1
    I guess the followup question would then be asking why you're using a custom field and not a category? Dec 3, 2012 at 20:45
  • I'm new to EE. Should "yellow" have been a category instead? If so, how do I do that? I just used a simple Select Dropdown to contain the thing I wanted to group by. Dec 3, 2012 at 20:48
  • Yes. I'll put together an answer for you, standby. Dec 3, 2012 at 20:58
  • Thanks. I'm looking at ellislab.com/expressionengine/user-guide/modules/channel/…, but I'm not sure if this actually shows me how to set things up. It would be great if I could somehow convert my Select Dropdown to this category feature. Dec 3, 2012 at 21:04
2

As others have noted comments could be the best way to go for you. But if you have valid reasons to use a custom field for fruit colors you can group entries by color with the third-party NSM Channels add-on (AUD $39.95)

This should work:

{exp:nsm_channels:group_entries_by
  group_by="custom_field_color"
  }
  <h3>{custom_field_color}</h3>
  <ul>
  {channel_entries channel="fruit"}
    <li>{title}</li>
  {/channel_entries}
  </ul>
{/exp:nsm_channels:group_entries_by}
1

Is there a specific reason you can't use the entries order by parameter to order by that field? I think understanding that would help us answer this question.

1

You could use Stash to filter by colour on output with match/against:

{exp:stash:set_list name="fruit" parse_tags="yes"}
    {exp:channel:entries channel="fruit"}   
        {stash:title}{title}{/stash:title}
        {stash:color}{cf_color}{/stash:color}
    {/exp:channel:entries}
{/exp:stash:set_list}

{exp:stash:get_list name="fruit" match="#^yellow$#" against="color"}
{title}
{/exp:stash:get_list}

https://github.com/croxton/Stash/tree/dev

2
  • Note that a category group for 'Colors' would be a more appropriate grouping solution, unless you have a particular reason to use a custom field. Dec 3, 2012 at 21:14
  • No particular reason for using Select Dropdown. I'm still navigating my way around EE. Will look into the categories solution first. Dec 3, 2012 at 21:20
0

Have a look at http://devot-ee.com/add-ons/wb-category-select field type It creates a drop-down of one or more category group.

Also one of the best category plugins around - http://devot-ee.com/add-ons/gwcode-categories you can use it to generate nested or simple list of categories or custom category fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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