ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Displaying Categories in Select/Drop-down Menu

October 11, 2012 9:58am

Subscribe [2]
  • #1 / Oct 11, 2012 9:58am

    ctrlaltdel

    119 posts

    Hello all,

    This is a little difficult to explain, so please bear with me!

    I’m working on a chamber of commerce website in which we’re displaying a list of the local businesses who are chamber members.

    Each business can be placed into a “business type” category, and the categories follow a parent/child relationship. Here’s an example of the site’s category structure:
    —-
    Finance & Insurance
      Financial Advisors
      Financial Institutions
      Insurance
      Mortgage Companies

    Health Care
      Adult Daycare
      Assisted Living Facilities
    —-

    Above, “Finance & Insurance” and “Health Care” are the parent categories, and the items underneath them are the child categories.

    I currently have the site displaying the entire list of categories in this parent & child hierarchy, and the categories are rendered in a <select> with an alphabetical sort order based on the parent first and the children below. So:

    A
      a
      b
      c
      d

    B
      a
      b
      c
      d

    C
      a
      b
      c

    The client is asking me if it’s possible to “ignore” the hierarchy and simply display ALL of the categories in alphabetical order, regardless of the parent/child relationship. Is this possible out-of-the-box? Do I need to get creative with some PHP and querying?

    If PHP is the way to go, could someone point me in the direction I’d need to go with this?

    Thanks in advance!

  • #2 / Oct 11, 2012 2:28pm

    mark186282

    290 posts

    What is the code you are currently using to give the nested nature of the categories?

    using the category tag “style” parameter:
    http://ellislab.com/expressionengine/user-guide/modules/channel/categories.html#id5


    You should just be able to do this:

    Assumes your category group of business categories is “1”

    <select>
    {exp:channel:categories category_group="1" style="linear"}
        <option value='{category_id}'>{category_name}</option>
    {/exp:channel:categories}
    </select>
  • #3 / Oct 13, 2012 8:08am

    ctrlaltdel

    119 posts

    Here’s the code that I’m using:

    <form id="category-select" name="catmenu" action="">
      <select name="selcat">
             <option value="">--Select Category--</option>
             {exp:channel:categories channel="chamber_members" style="linear"}
                 {if parent_id == '0'}{if count != '1'}</optgroup>{/if}<optgroup label="{category_name}">{/if}
                 {if parent_id != '0'}<option value="{path='chamber-members/category'}">{category_name}</option>{/if}
             {/exp:channel:categories}
         </select>
     </form>

    That code gives me (in my opinion!) a nice hierarchical select menu as I described earlier. It visually keeps the parent/child relationship because of the <optgroup> element.

    Mark, just as a test, I tried the code that you suggested, and the menu is pretty much the same, but without the <optgroup>. The categories are still listed alphabetically first by parent and then by children.

    I’d like to display all of the categories, regardless of them being a parent or a child, in alphabetical order.

    Thanks!

  • #4 / Oct 14, 2012 9:39am

    Kevin Smith

    4784 posts

    I do like your hierarchical select menu, ctrlaltdel! But I certainly understand meeting the client’s wishes here. There’s not a way to have the native tags display the output like you want unfortunately, so you’ll likely need to write up a custom plugin to handle this or use some PHP in the templates to do it. That’s more than I can really help you with here, but I’d be glad to move this over to Development & Programming so some friendly community developers can help you come up with a solution. Would you like that?

  • #5 / Oct 15, 2012 7:04am

    ctrlaltdel

    119 posts

    Kevin,

    That would be awesome! Thank you!

  • #6 / Oct 15, 2012 7:23am

    ctrlaltdel

    119 posts

    Actually, Kevin, I think I have already solved this with a third-party add-on that was suggested in another thread that I found.

    I’m not the creator of this add-on, so don’t worry, I’m not trying to SPAM the forums!

    The add-on is called GWCode Categories (http://gwcode.com/add-ons/gwcode-categories/). I reached out to the developer on Twitter and he indicated it would be as simple as adding an “orderby” parameter to the tag pair. I did that and changed the path in my <option> value attributes, and it worked like a charm.

    Here’s my final code:

    <form id="category-select" name="catmenu" action="">
      <select name="selcat">
             <option value="">--Select Category--</option>
                 {exp:gwcode_categories channel="chamber_members" orderby="cat_name"}<option value="{path='chamber-members/category/name/{cat_url_title}'}">{cat_name}</option>{/exp:gwcode_categories}
         </select>
     </form>

    Again, thanks for the help, Kevin and for offering to move this over to the other forum. I appreciate it. The EE community comes through again!

  • #7 / Oct 15, 2012 12:56pm

    Kevin Smith

    4784 posts

    No worries, ctrlaltdel, GWCode Categories is a handy add-on! Glad to hear it worked for you here. Anything else we can help with?

  • #8 / Oct 15, 2012 1:42pm

    ctrlaltdel

    119 posts

    I think that’s going to be it! Feel free to close the thread if that’s the standard practice.

  • #9 / Oct 16, 2012 12:01pm

    Kevin Smith

    4784 posts

    Great, have a good one ctrlaltdel!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases