Embedded Related Entries
I came across the problem the other day of how to show a related entry of a reverse related entry of my entry. Confused? Imagine the following scenerio:
You have two separate weblogs, A and B. Each has several entries and are related to each other through a third weblog C. The weblog C holds all the relations and weblog A and B do not have any relations. This means that there can be many entries in C which point to A, and many entries in C that point to B. Still confused? Here is a real world example.
You have several lawyers who practice in a variety of practice areas but not necessarily all areas. The same relation is true for practice areas, where on area can have several attorneys but maybe not all. Each attorney has a statement about his expertise in the practice areas they serve in. In this example, the Lawyer is held in weblog A, the practice area in B and the statement of expertise in weblog C.
Now, lets say you would like to have an Lawyer page for a specific lawyer in weblog A, and you would like to list out all the practice areas (B) on the lawyers page. Because there is no direct relation between A and B you must use the relation between A – C and then the relation from C – B to get the final relation of A – B. To do this you must use a nested related entry in a reverse related entry for the entry of the Lawyer.
Unfortunately, expression engine does not allow nested related items on a page directly. So to make this work, you must use an embed to make the nesting work. Here is the code for the lawyer page:
{reverse_related_entries orderby=”title”}
<div class=”entry”>
{embed=”site/attorney_link” entry_id=”{entry_id}”}
<h3>{title} – {attorney}</h3>
<p>{about_bio}</p>
</div>
{/reverse_related_entries}
All of the above code would be held inside the {weblog:entries} tag for the specific lawyer. Now the code for the embedded file:
{exp:weblog:entries dynamic=”off” entry_id=”{embed:entry_id}”}
{related_entries id=”attorney”}<h4><a href=”{url_title_path=”attorney”}”>{title}</a></h4>{/related_entries} {/exp:weblog:entries}
The key to making the nested related entry work, is to use the embed method. This allows you to send the entry id of the outer reverse related into a separate page. On the new page, you can do a normal exp:weblog call searching for only that id that was passed through. This will then give you access to the post in the intermediary blog (C) and allow you to call a related entry for either A or B.
If you are still confused after reading this, send me a comment and maybe I can clarify a little further. While this may not be something you use everyday, it definitely can come in handy.
Thanks for posting this solution. When I try it, my page is printing out the related entries tags inside the embed, instead of executing the related entry query. Are there any template settings that need to be tweak to get this up-and-running?
I dont have access to an expression engine install any more but it does sound like something is not setup properly for the embedded file. An embedded link should parse any ee tags as normal, but I think when you create the templates you can set what type of template it is, and there are some options that will render the file as static and not parse anything. Let me know if you need more help with this, sorry for the delay. You can also use my contact form on sidewaysgravity.com if you wish to correspond directly.