Skip to:
Content
Pages
Categories
Search
Top
Bottom

Nested topic list code snippet

  • I figure someone might find this useful, as it took me a while to do properly. The following is some code I’m using to create a bullet point (<ul>) list of forums which nests sub-forums nicely, in my case for a sidebar.

    <?php if ( bb_forums() ) :
    while ( $depth = bb_forum() ) :
    if( $depth > $last_depth ) : ?>

    <?php echo str_repeat(" ",$depth) ?><ul>
    <?php else : ?></li>
    <?php while( $depth < $last_depth ) :
    echo str_repeat(" ",$depth+1) ?></ul>
    <?php echo str_repeat(" ",$depth) ?></li>
    <?php $last_depth--;
    endwhile;
    endif;
    if (bb_get_forum_is_category()) :
    echo str_repeat(" ",$depth) ?><li class="forum_cat"><a href="<?php forum_link() ?>" title="<?php forum_description(array('before'=>'','after'=>'')) ?>"><?php forum_name() ?></a><?php
    else :
    echo str_repeat(" ",$depth+1) ?><li class="forum_subforum"><a href="<?php forum_link() ?>" title="<?php forum_description(array('before'=>'','after'=>'')) ?>"><?php forum_name() ?></a><?php
    endif;
    $last_depth = $depth;
    endwhile ?></li>
    </ul>
    </li>
    </ul>
    <?php endif; // bb_forums() ?>

    It should give you reasonably clean indenting of HTML too and is XHTML 1.0 valid. Take out the str_repeat() calls if you don’t care about tidy code.

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.