表达式引擎类别 - 需要帮助使用 if 语句添加动态类


ExpressionEngine categories - Need help adding dynamic class using if statements

这是一个

简单的问题,但在过去的两个小时里,我一直在拔头发,试图找到一种方法让它工作。

基本上我有一个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;" />

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

您希望根据 URL 中的类别 URL 标题进行计算。我不完全熟悉您正在使用的儿童类别插件,但似乎这应该在您的{exp:child_categories}循环中工作:

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

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