January 5, 2015

Dynamic Categories On Subpages in ExpressionEngine

During a recent project, one of ExpressionEngine’s downfalls quickly forced me to think outside of EE’s built-in template syntax. The task was to allow the user to set ANY page to 1) A channel listing page (of any channel) 2) To START from any selected category and 3) Allow users to drill down into any category below the selected parent category. On the surface, this seems like it should be an easy task, particularly if you are not accustomed to ExpressionEngine’s syntax and limitations. Luckily, this downfall allows us to tackle this obstacle from many different approaches.

First, lets look at the template from which we will be calling our new template:

{exp:channel:entries channel="static_pages" disable=" pagination|trackbacks" require_entry=—yes— } 

{!–insert attached sidebar widgets–} {embed=”_structure/_sidebar_widgets” entry_id=”{entry_id}”} {!–insert attached sidebar widgets–}

  • {embed=”_structure/categories_subpagefiles categoree=”{document_category}{category_id}{/document_category}”}

{/exp:channel:entries}

You’ll notice that we are requiring an entry to pull our initial data from, i.e. our —static page— or —subpage—. Which is essentially just a page to house all basic data (title, body text, sidebar elements,), along with the information to let EE know which entries to display (channel, category). You’ll also notice the snippet in which we will be referencing our template where the magic happens:

{embed="_structure/categories_subpagefiles categoree="{document_category}{category_id}{/document_category}"} 

Here we are calling a new embed variable called —categoree— and setting it equal to a WB Category Select field, which will enable the user to select the category of entries to display on the page. Since EE needs category ID’s and not simply the name of the category, we have set this variable to the category ID of said category which the user will select.

The other half of this template (I’ll just include the relevant bit), is how we are going to set a category by using a GET variable. The reason why we need to set the category via a GET variable and not by passing it as a category segment, which contains either the url title or id of a category, is because if we do then EE will easily get lost on which page to return to retrieve our original page.

<a href="#SubMenu1-%7Bcat_url_title%7D" class="list-group-item collapsed {if segment_2 == '{cat_url_title}'}active{/if}">{if has_children_in_output}<i class="fa fa-caret-down"></i>{/if}<span class="accordion-link">{cat_name}</span></a> 

This is simply just appending a GET variable containing the url title of each category in the sidebar. We will deal with this variable on the next template.

  • <?php $no_get = true; $param = ”; foreach ($_GET AS $key => $value) { if (strtolower($key) != ‘p’) { $no_get = false; } } if ($no_get) { $param = ‘category=”{embed:categoree}”‘; } else { if (isset($_GET[‘cat’]) && is_array($_GET[‘cat’])) { //We are viewing a sub-category more specific/deeper than selected in entry field and it is not a pagination GET variable foreach ($_GET[‘cat’] AS $url_value) { $this->EE->db->select(‘exp_category_posts.entry_id’); $this->EE->db->from(‘exp_categories’); $this->EE->db->from(‘exp_category_posts’); $this->EE->db->where(‘exp_categories.cat_id = exp_category_posts.cat_id’); $this->EE->db->where(‘exp_categories.cat_url_title’, $url_value); $query = $this->EE->db->get(); $entry_ids = array(); foreach ($query->result() as $row) { $entry_ids[] = $row->entry_id; } $returned_list_of_entry_ids = implode(‘|’, $entry_ids); $param =”entry_id='”.$returned_list_of_entry_ids.”‘”; } } } ?> {exp:channel:entries channel=”files_documents” disable=”member_data|trackbacks” dynamic=”no” paginate=”bottom” limit=”5″ <?php echo $param; ?> } {if no_results}

No Documents

{/if}

{title} {if sticky == ‘y’}icon {/if}

{file_description}

And finally, we have where the entry retrieving actually happens. This is what is going on in this snippet:

  1. We are first checking to see if a GET variable even exists in the URL. If it doesn’t, we know we are simply on the original subpage, and thus we are on the highest level category and we don’t need to do anything special since we already have the category ID we selected earlier from the entry, specifically in the WB Category Select field.
  2. If a GET variable does exist, we need to also make sure that we are not somehow on a pagination page (further security).
  3. We are setting our entire results in PHP equal to that of an entire EE channel entries parameter. Specifically, the entry list parameter. This way, if we aren’t specifically providing a list of relevant Entry_ID’s this parameter will simply ignore this, and work as normal by just selecting the category ID from the field.
  4. If the GET variable does indeed exist, we need to perform a few active record queries in order to provide EE a list of Entry_ID’s that match the category selected by the web user via the Category Sidebar widget. We simply grab the URL title from the variable, and perform a lookup to retrieve the category ID via an inner join method between categories and categories_posts tables and cat_id and cat_url_title columns. This gives us an Entry_ID array full of relevant Entry_ID’s separated by a pipe character.

It seems like a bit of work for such a simple task. But the fact is, unless you are able to assume at which segment these pages will exist, this method is entirely needed. Otherwise, we could just create a template group titled appropriately to our URL segments and any relevant templates. This setup was quickly shot down by the fact that this functionality had to truly be dynamic. With some ingenuity and a few lines of some PHP code, we were able to accomplish our results without having the need to extend our existing custom modules and extensions.

Don’t forget to enable PHP on this template, and happy coding!

Paul Weinert
Author

Paul Weinert is our founder, managing principal and spirit animal. He’s one of those odd people you meet that was born in Portland, grew up here and still lives here for some reason. If you had two words to describe Paul after meeting him, we’d bet you’d use “charmingly quirky”.

Paul started GRAYBOX in February 2009, and for the first four years, GRAYBOX had a blend of a contract staffing firm and a traditional agency. GRAYBOX then hired our first employees in 2012 and has been growing ever since. Professionally, Paul has a background in internet business consulting, working especially with mid-market ecommerce, healthcare, and tech companies. He worked as a user experience designer, web developer and internet marketer. Nowadays, Paul serves as our company visionary, team mentor and head of executive leadership — Paul is extremely proud of the entire team and the company. He’s constantly humbled by the team’s awesomeness and the caring community that’s sprung up from his initial ideas.

Beyond GRAYBOX, Paul is married, has two young boys and lives in NE Portland. He’s also an active foster parent, mentors other local entrepreneurs, and volunteers on local non-profit boards. Businesswise, aside from GRAYBOX, Paul has started a few other businesses and is an active investor in the local startup scene.

For the sheer joy of it, let’s play 2 truths and a lie:

  1. Paul firmly believes that any activity would be better if on a boat.
  2. Other than this whole Internet thing, Paul has basically no other skills or expertise in anything.
  3. Paul is rare unicorn, actually born and raised in Portland.