January 5, 2015
Dynamic Categories On Subpages in ExpressionEngine

January 5, 2015

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–}
{/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.
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:
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!