Using Related Entries to Set Sidebar Content in Expression Engine
Among the most useful features added to Expression Engine in the past year is Related Entries, or Data Relationships. This allows you to further section off content and data and use it in useful ways by creating relationships between weblog entries. One simple way I've started using this feature is to provide sidebar content that content editors and authors can relate to their entries. Details after the jump.
So, a typical layout on many websites is the two column content+sidebar configuration. If you’re reading this on our site, you’re looking at such a layout. Typically, content is displayed in the larger area (the right side of this page) and the slimmer sidebar (on the left) is reserved for stuff like navigation, related links, or supplemental info. A common approach to sidebar includes the sort of content that is often reused for numerous pages. If you click around in the blog section of this site, you’ll see what I mean.
In Expression Engine, I’ll use the {embed} tag to section off bits of content for use on more than one template. This works pretty much like a server-side include and is certainly a nice way of keeping things tidy and allow for easy updates to global elements — an update to an {embed} updates it anywhere is occurs on the site. The big drawback to this is that if a content editor wants to change something, I either have to give them access to the templates (risky) or make the change myself (costly for the client, among other drawbacks).
With Related Entries, we can do pretty much the same thing with added flexibility. By making the sidebar items a weblog entry, content editors can create as many of them as they want, edit them as they see fit, and associate them with the primary content entries where they will have the most impact. On the recently launched Where2GetIt site, I used this approach. Sidebar items are used on interior pages and include things like newsletter signup forms, contact info, links to demos, and product-specific information.
Setting this up is incredibly straightforward. You’ll need to set up a weblog for your primary content and one for your sidebar items. For sidebar items on the W2GI site, I created a weblog with two custom fields — one for Content and another for an Alternate Title…
Alternate Title is used in this case to give author the ability to use a title that makes sense for keeping track of the entry in the EE control panel (via the standard Title field) and one that would display on the site. For this client, there were a handful of sidebar entries that had the same title, but contained slightly different, so things would have gotten confusing in the control panel. In most cases, you could probably do without the Alternate Title field.
The main areas of the site, which includes the Blog and a Pages section among others, contain three custom relationship fields.
Depending on the substance of the entry, the site’s content editor can choose the most appropriate secondary content to go along with it. Anything from newsletter subscribe forms to ads for related services.
The template code for the sidebar looks like this:
"}
{if pages_sidebar1}
{related_entries id="pages_sidebar1"}
<div class="sidebar">
<h3>{title}</h3>
{sidebar_content}
</div>
{/related_entries}
{/if}
{if pages_sidebar2}…
...
I’ve edited out the second and third sidebar items, but you get the idea. EE is basically looking to see if any related entries have been selected and displaying the content of those that have. Pretty simple and very handy!
Since starting this article, I have come across Mark Huot’s very good Multi Relationship extension. Instead of drop-down lists where the content editor can only choose one entry, the extension allows multiple selection to be made. Since it loops through for as many entries as selected, the template code gets simplified (no need to check for each field). You would also not be limited to three sidebar selections. The only drawback I can see is that there is no way to sort the order in which the related entries get displayed. In the case of the W2GI site, the extension wouldn’t have worked out.
Data Relationships continues to be one of the most useful additions to Expression Engine. This is a pretty simple application of the feature, but one that is immensely useful to site owners and content managers.
Phil Hertzler | 02.19.07 | 5 comment(s)
Reader Comments
Thanks for the reply, Mike. I ended up using Playa; it worked a charm for me. Almost ideal, in fact.
Posted by Luke McReynolds from Grand Rapids, MI on 10.07.08 at 2:58
very nice info, thanks!
Posted by Max Glipmax from Los Angeles on 06.26.09 at 9:32
Any word on whether or not it's possible to sort the display order of the related entries? I'm using the Multi-Relationship extension as well, and I haven't been able to find a way to sort the related entries at all. Maybe something's come up since you wrote this post.
Posted by Luke McReynolds from Grand Rapids, MI on 09.03.08 at 9:29