Search code examples
htmlexpressionenginecategoriesconditional-statements

Embedding Templates in the Header File Based On Categories


I am using ExpressionEngine 2.2.1 and am trying to create a conditional that will cause a page to show a specific navigation template based on the page's category. For example, I am creating a public profile for someone who is a part of a competition in London. When someone visits his profile, I would like the page to show, not only his profile, but also a banner with an image of London & navigation that is specific to the London competition. I would like to do this by adding a conditional with categories since we have so many competitors in many different cities. I have created a category titled "London Competitors English" and it has the ID of "56". Once I have created the competitor's profile within EE, I select the category, "London Competitors English," however, when I go to the page, the default navigation template is loading rather than the London template. I created code based on my research and most related example here: http://expressionengine.com/forums/viewthread/185555/. What do I have wrong in my code?:

FILE ONE: (condensed portion of) competitor_profiles_en/index.html where "competitor_profiles_en" is the template:

{exp:channel:entries  limit="1" url_title={segment_2} }
{embed="includes/header"}
<div class="xx">    
<h2>{competitor_first_name}<br>{competitor_last_name}</h2>      
...
</div>
{/exp:channel:entries} 

FILE TWO: (condensed portion of) my header.html file :

{if segment_1 == 'home'}
{embed="includes/_nav_landing"}
{if:elseif "{categories}{category_id}{/categories}" == "56"}
{embed="includes/_nav_city_lon_en"}
{if:else}
{embed="includes/_nav_internal"}
{/if}

Solution

  • The problem is that embeds are processed after all other tags, in a completely separate process. So when your includes/header embed runs, it has no categories to speak of, because it's not actually running within a Channel Entries tag.

    Unless you need to pass variables to includes/header (which you don't appear to be doing here, but you may have removed that for simplicity), I'd move your includes/header to a snippet instead, so it will be processed inline with your Channel Entries tag.