{exp:weblog:entries weblog="{embed:my_weblog_name}" url_title="not {embed:my_index_entry}" sort="asc" dynamic="off"}
{!-- This first clause in the if statement finds all entries that have no parent - assumed to be the top-level navigation --}
{if {embed:my_relationship_field}=="0"}
-
{title}
{!-- This clause builds subnavigation for the currently-viewed page--}
{if url_title==segment_3}
{reverse_related_entries weblog="{embed:my_weblog_name}" sort="asc"}
{if count==1}
{/if}
- {title}
{if count==total_results}
{/if}
{/reverse_related_entries}
{/if}
{!-- This clause is activated when a 3rd level entry is being viewed, and is essentially a "find siblings" query - finding other entries related to the same parent entry --}
{if:elseif {embed:my_relationship_field}!="0" AND segment_3==url_title}
{exp:query sql="SELECT exp_weblog_titles.title as my_title, exp_weblog_titles.url_title as my_url_title
FROM (exp_relationships INNER JOIN exp_relationships AS exp_relationships_1 ON exp_relationships.rel_child_id = exp_relationships_1.rel_child_id)
INNER JOIN exp_weblog_titles ON exp_relationships_1.rel_parent_id = exp_weblog_titles.entry_id
WHERE (((exp_relationships.rel_id)={{embed:my_relationship_field}}) AND exp_weblog_titles.weblog_id={weblog_id}) ORDER BY exp_weblog_titles.entry_date asc;"}
- {my_title}
{/exp:query}
{/if}
{/exp:weblog:entries}