Get Current Forum ID?
-
I’m attempting to create a side nav of all the forums on my site. When a user is viewing a certain forum, I want that “tab” to be highlighted using CSS.
All I need is to assign a class=”on” attribute to the forum [li] [/li] tag if it is the current forum being viewed. Anyone know how I can check the current forum against the forum id?
Here’s the code for my menu creation:
<?php if ( bb_forums() ) : ?>
<h3><?php _e('Forums'); ?></h3>
<ul>
<li <?php if ( is_front() ) : ?>class="on"<?php endif; ?>><a>">Overview</a>
<?php while ( bb_forum() ) : ?>
<li><a>"><?php forum_name(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; // bb_forums() ?>
-
This is pretty similar, I think:
https://bbpress.org/forums/topic/hiding-subforums-subforums-from-subforum-pages#post-17436
Thanks for the suggestion. It didn’t fix what I am trying to do.
Basically, while the php is executing the
while ( bb_forum() ) :
loop, I need to check the current forum against the forum it is writing to the menu.If there are 4 forums in the while loop, I need it to check the current forum id against each forum id as it is writing it to html. If the current forum id matches the forum id it’s writing, then it would add the
class="on"
attribute to theli
tag.Untested and YMMV:
$current_forum_id = get_forum_id();
while ( bb_forum() ) {
$class = '';
if ($current_forum_id == get_forum_id()) {
$class = 'on';
}
}Thanks! Worked perfectly.
Hi guys, i’m trying to get the above code to work without any success.
This is the code i’m using in my header files:
<?php
$current_forum_id = get_forum_id();
while ( bb_forum() ) {
$class = ”;
if ($current_forum_id == get_forum_id()) {
$class = ‘on’;
}
}
?>
…and this is the code i’m using in my front page and forum template (within the menu):
<li class="<?php echo $class; ?>">
Not sure where i’m going wrong, any help would be much appreciated.
Thanks
Why not just do it all in the li:
<li<?php if ($current_forum_id==get_forum_id()) {echo " class='on'";} ?>>
I modified your code to the following and it worked:
<li<?php if ($current_forum_id=get_forum_id()) {echo ” class=’on'”;} ?>>
Thanks a lot CK, much appreciated.
Sorry guys, I thought my problem was resolved but it looks like i’ve spoke to soon.
The code _CK_ suggested below, worked wonders to add a new “on” class on my forum links:
<li<?php if ($current_forum_id=get_forum_id()) {echo " class='on'";} ?>>
Problem!
The trouble is that the unique “on” class is being displayed on every forum link, when I need it to only display when i’m viewing forum 2 for example. Basically, i need a unique css class and current forum marker so the user knows which forum they are currently viewing.
Below is the souce code for all of forum.php (if anyone has time to have a quick look i’d really appreciate it).
<?php bb_get_header(); ?>
<h3><?php forum_name(); ?> Forum</h3>
<ul id=”forumnav”>
<li<?php if ($current_forum_id=get_forum_id()) {echo ” class=’on'”;} ?>>” title=”Latest homepage”><span>Latest<img src=”images/white-arrow.gif” style=”padding: 0 0 3px 5px” width=”10″ height=”5″ border=”0″ /></span>
<li<?php if ($current_forum_id=get_forum_id()) {echo ” class=’on'”;} ?>>forum/forum2″ title=”Forum 2″><span>Forum 2</span>
<li<?php if ($current_forum_id=get_forum_id()) {echo ” class=’on'”;} ?>>forum/forum3″ title=”Forum 3″><span>Forum 3</span>
<li class=”<?php echo $class;?>”>forum/forum3″ title=”Forum 3″><span>Forum 3</span>
<?php if ( $topics || $stickies ) : ?>
<table id=”latest”>
<tr>
<th><?php _e(‘Topic’); ?> — <?php bb_new_topic_link(); ?></th>
<th><?php _e(‘Posts’); ?></th>
<th><?php _e(‘Last Poster’); ?></th>
<th><?php _e(‘Freshness’); ?></th>
</tr>
<?php if ( $stickies ) : foreach ( $stickies as $topic ) : ?>
<tr<?php topic_class(); ?>>
<td><?php bb_topic_labels(); ?> <big>“><?php topic_title(); ?></big></td>
<td class=”num”><?php topic_posts(); ?></td>
<td class=”num”><?php topic_last_poster(); ?></td>
<td class=”num”>“><?php topic_time(); ?></td>
</tr>
<?php endforeach; endif; ?>
<?php if ( $topics ) : foreach ( $topics as $topic ) : ?>
<tr<?php topic_class(); ?>>
<td><?php bb_topic_labels(); ?> “><?php topic_title(); ?><?php topic_page_links(); ?></td>
<td class=”num”><?php topic_posts(); ?></td>
<td class=”num”><?php topic_last_poster(); ?></td>
<td class=”num”>“><?php topic_time(); ?></td>
</tr>
<?php endforeach; endif; ?>
</table>
<div id=”viewdiv”>
<ul id=”views”>
<li class=”view”>” class=”rss-link”><?php _e(‘<abbr title=”Really Simple Syndication”>RSS</abbr> feed for this forum’); ?>
</div>
<div class=”nav”>
<?php forum_pages(); ?>
</div>
<?php endif; ?>
<?php if ( bb_forums( $forum_id ) ) : ?>
<h2><?php _e(‘Subforums’); ?></h2>
<table id=”forumlist”>
<tr>
<th><?php _e(‘Main Theme’); ?></th>
<th><?php _e(‘Topics’); ?></th>
<th><?php _e(‘Posts’); ?></th>
</tr>
<?php while ( bb_forum() ) : ?>
<?php if (bb_get_forum_is_category()) : ?>
<tr<?php bb_forum_class(‘bb-category’); ?>>
<td colspan=”3″><?php bb_forum_pad( ‘<div class=”nest”>’ ); ?>“><?php forum_name(); ?><small><?php forum_description(); ?></small><?php bb_forum_pad( ‘</div>’ ); ?></td>
</tr>
<?php continue; endif; ?>
<tr<?php bb_forum_class(); ?>>
<td><?php bb_forum_pad( ‘<div class=”nest”>’ ); ?>“><?php forum_name(); ?><small><?php forum_description(); ?></small><?php bb_forum_pad( ‘</div>’ ); ?></td>
<td class=”num”><?php forum_topics(); ?></td>
<td class=”num”><?php forum_posts(); ?></td>
</tr>
<?php endwhile; ?>
</table>
<?php endif; ?>
<?php post_form(); ?>
</div> <!– End Content –>
<?php bb_get_footer(); ?>
Thanks again sambauers, _CK_ and other mods – you’re doing a great job.
What version bbPress are you using?
I can’t figure where you want to use this class of ‘on’ to even think about why it’s not working. Can you show a page where you have a forum id (current_forum_id) , but also a list of forums that you would be applying this class to? I can’t grok it.
Thanks
Thanks for your reply chrishajer, much appreciated & sorry about late reply (i got bogged down with some other work). I am using the latest unstable version 1.0. I am using this version because the site is not finished so I don’t expect to launch it for a while – I thought I might as well get use to the changes while I can.
Anyway, the site in question is http://betting-forums.net/beta/
On the homepage the latest tab is current dark grey/black. When a user clicks the racing tab (or anyother forum tab) I want that tab to change colour to dark grey/black – basically so that the user knows what forum they are currently are on.
Any suggestions would be much appreciated.
The problem is quite simple, really.
<li<?php if ($current_forum_id=get_forum_id()) {echo " class='on'";} ?>>
just needs to be changed to<li<?php if ($current_forum_id==get_forum_id()) {echo " class='on'";} ?>>
(Can you spot the difference?)
thanks for trying to help nightgunner but unfortunately this still displays the”on” class for every link in the menu, when I only need it to display the “on” class for the forum the user is currently viewing.
- You must be logged in to reply to this topic.