ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Category Display Parent Class

November 04, 2008 11:05am

Subscribe [2]
  • #1 / Nov 04, 2008 11:05am

    aaronvegh

    9 posts

    Hi there,
    I am printing out a category list that has two levels: parents and children. The list is rendered as a UL, which is fine, but I want to be able to assign a CSS class to the parents only. Is there any way to know when a particular item is a parent so I can assign a class? This is what my code looks like now:

    {exp:weblog:categories style="nested" parent_only="no" class="head"}
                    <a href="#">{category_name}</a>
    {/exp:weblog:categories}

    I would like to assign the class “head” to just the parents. It seems EE knows, but I can’t find a handle to tell me…

    Thanks,
    Aaron.

  • #2 / Nov 04, 2008 11:08am

    Boyink!

    5011 posts

    In the past I’ve had EE return a nested list, then just deal with the nests in CSS:

    Parent:
    ul {}
    ul li {}

    Children:
    ul li ul {}
    ul li ul li {}

    Ect…

  • #3 / Nov 04, 2008 11:37am

    aaronvegh

    9 posts

    thanks for the reply. Unfortunately I am using jquery to animate the nav, and it doesn’t seem to work using those kinds of selectors. I sure hope it’s possible to give the parent its own class!

    Thanks,
    Aaron

  • #4 / Nov 06, 2008 1:58pm

    Adrienne L. Travis

    213 posts

    There are a couple of third party plugins that do similar things; one might be able to be made to suit your needs. Check the Plugins forum here and search for ‘category’.

    Alternatively you can just use the Query module to get categories, which I’ve done before, but it’s a lot more of a pain. Check plugins first. 😊

  • #5 / Nov 06, 2008 2:47pm

    aaronvegh

    9 posts

    It turns out I had to go with PHP directly in order to solve this problem. I feel like a total h@X0r doing this, but I couldn’t come up with an alternative that worked!

    I placed my navigation code inside its own template; I’ll embed it into my header template. Here’s what it looks like:

    <?php
                global $DB;
                $query = $DB->query("select * from exp_categories where parent_id=0");
                foreach($query->result as $row) {
                    ?>
                    <h2><a href="#" class="head"><?=$row["cat_name"]?></a></h2>
    <p>                <?<br />
                    $parentid = $row["cat_id"];<br />
                    $sub = $DB->query("select * from exp_categories where parent_id=".$parentid);<br />
                    if($sub) {<br />
                        ?><br />
                        <ul><br />
                        <?<br />
                        foreach($sub->result as $row2) {<br />
                            ?><br />
                            <li><a href="{path='mdr/product_category'}category/<?=$row2["><?=$row2["cat_name"]?></a></li><br />
                            <?    <br />
                        }<br />
                        ?><br />
                        </ul><br />
                        <?<br />
                    }            <br />
                }<br />
                ?>

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases