Three Tier Static Content in ExpressionEngine - Approach D
Here’s a fourth way to build navigation for static content in ExpressionEngine, using one weblog, one template, no categories, and relationships. Previously I’ve posted 3 other ways to do this on a mini-site. The advantages to this newest approach is everything can be contained in one weblog, using one template, no categories, and one relationship field. Here’s a working example of this approach.
This method builds nicely on the “Approach-A” I’ve outlined previously. Here’s what you need to add:
- Another custom field, named “static_parent_page”. Make this a relationship field and specify the same weblog as this fieldset is already used in. Stick with the default for title, and save the new field
- Now go through your content entries, and edit the second-level pages, assigning them to the parent-level pages. In my working example I have seven entries. I left the index entry alone, and edited pages two, three, five and six to assign them to either page one or four - which I’m arbitrarily making my second-level pages.
The nav-building code is a bit more complex in this case, so lets take a look at the text version of the template and go through the code.
- The main weblog:entries tag that builds the left nav is unchanged from Approach A
- The first conditional looks at the value of the new static_parent_page field. If this is 0, that means there is no relationship setup, so this must be a parent page.
- Each parent page gets an opening li, then the code uses reverse related entries to pull in all the pages that list this page as parent. Reverse related entries is necessary because we estabished the relationship from the perspective of the child
- The code uses two EE variables, count and total_count, to determine when to insert the nested opening and closing ul tags.
- Once the reverse_related_entries code is done, the closing li tag for the parent item is added.
- The weblog entries tag closes and then the overall parent ul tag is closed.
The results of this approach can be seen in the working example, which renders with an index entry that’s omitted from the left-nav (the top-level of content), and a navigation bar that has two levels of pages. The second level links are pages with no relationships established thereby making them “parents”, and under each parent is a nested UL with it’s children (the entries where we specified a parent page).
- Simplicity in terms of EE setup - using only one weblog and one template
- Better usability for clients—the parent_page field appears on the Control Panel Publish form along with other fields, vs. the categories which depend on users remembering to click to another tab to assign.
- Consistent interface for content maintenance—all using the weblog Publish page, vs. using categories where category pages have to be edited in the category interface where there is no file upload function, etc.
- Subpage URL structure is the same as Parent Pages—so the nav structure is not reflected in the URL structure.
- The Parent Page drop-down won’t reflect any structure, so this approach is probably best for smaller less complex sections of content.
Overall I think this is still my favorite approach for static three-level content in EE - especially when the structure can be filled in by the site developer before the client is unleashed on the site.