ExpressionEngine类别 - 需要帮助使用if语句添加动态类

时间:2012-04-16 15:55:59

标签: php if-statement categories expressionengine

这是一个简单的问题但是我在过去的两个小时里一直在努力寻找一种方法让它发挥作用。

基本上我有一个jquery导航设置。我有4个选项卡,每个选项卡都是具有子类别的父类别。单击选项卡时,将显示包含子项的下拉列表。我已经完成了所有设置,但无法根据所选类别为子类别添加一类“活动”。

除非我已经消失,而且它正好坐在我眼前,我找不到明显的方法。我认为问题是,我没有硬编码导航链接,因为客户希望自己管理这些,所以我动态显示类别,添加一个'if'语句就是将if语句添加到所有的类别,以便将类添加到所有子选项卡。

这是我的代码:

<div id="tabbed-cats">
    {exp:channel:entries channel="product"}
    <ul class="tabs">
        <li class="nav-one"><a href="#bathroom" {categories}{if category_id == "1"}class="current"{/if}{/categories}>Bathroom</a></li>
        <li class="nav-two"><a href="#homecare" {categories}{if category_id == "2"}class="current"{/if}{/categories}>Homecare</a></li>
        <li class="nav-three"><a href="#transfer-equipment" {categories}{if category_id == "3"}class="current"{/if}{/categories}>Transfer Equipment</a></li>
        <li class="nav-four last"><a href="#mobility" {categories}{if category_id == "4"}class="current"{/if}{/categories}>Mobility</a></li>
    </ul>
    {/exp:channel:entries}

    <div class="list-wrap">
        {exp:channel:entries channel="product"}
        <ul id="bathroom" {categories}{if category_id == "2" OR category_id == "3" OR category_id == "4"}class="hide"{/if}{/categories}>
            {exp:child_categories channel="product" parent="1" category_group="1" show_empty="yes"}
            {child_category_start}
            <li><a {categories}{if category_id == "5"}class="active"{/if}{/categories} href="{path='products/category/{child_category_url_title}'}">{child_category_name}</a></li>
            {child_category_end}
            {/exp:child_categories}
        </ul>
        {/exp:channel:entries}

        {exp:channel:entries channel="product"}
        <ul id="homecare" {categories}{if category_id == "1" OR category_id == "3" OR category_id == "4"}class="hide"{/if}{/categories}>
            {exp:child_categories channel="product" parent="2" category_group="1" show_empty="yes"}
            {child_category_start}
            <li><a {categories}{if category_id == "6"}class="active"{/if}{/categories} href="{path='products/category/{child_category_url_title}'}">{child_category_name}</a></li>
            {child_category_end}    
            {/exp:child_categories}
        </ul>
        {/exp:channel:entries}

        {exp:channel:entries channel="product"}
        <ul id="transfer-equipment" {categories}{if category_id == "1" OR category_id == "2" OR category_id == "4"}class="hide"{/if}{/categories}>
            {exp:child_categories channel="product" parent="3" category_group="1" show_empty="yes"}
            {child_category_start}
            <li><a {categories}{if category_id == "8"}class="active"{/if}{/categories} href="{path='products/category/{child_category_url_title}'}">{child_category_name}</a></li>
            {child_category_end}    
            {/exp:child_categories}
        </ul>
        {/exp:channel:entries}

        {exp:channel:entries channel="product"}
        <ul id="mobility" {categories}{if category_id == "1" OR category_id == "2" OR category_id == "3"}class="hide"{/if}{/categories}>
            {exp:child_categories channel="product" parent="4" category_group="1" show_empty="yes"}
            {child_category_start}
            <li><a {categories}{if category_id == "7"}class="active"{/if}{/categories} href="{path='products/category/{child_category_url_title}'}">{child_category_name}</a></li>
            {child_category_end}    
            {/exp:child_categories}
        </ul>
        {/exp:channel:entries}
    </div><!-- END LIST WRAP -->
    <br style="clear:both;" />

2 个答案:

答案 0 :(得分:1)

在EE 2.4中,您现在可以使用{if active}class="foobar"{/if}尝试并查看。

答案 1 :(得分:0)

您希望根据网址中的类别网址标题进行计算。我对你正在使用的Child Categories插件并不完全熟悉,但似乎这应该适用于你的{exp:child_categories}循环:

{if child_category_url_tile == segment_3} class="active"{/if}

我并不完全理解您在代码中所做的事情,因为您调用相同的通道条目循环5次,然后每次根据条件类别语句过滤显示的内容。如果这实际上是您的完整代码,您应该认真考虑重新思考和重构。你的开销很疯狂!