Support » Fixing WordPress » Category Navbar Highlighting

  • At the top of my page is a navbar that uses tabs to display my categories. What I would like to do is have it highlight the current viewed tab, which I’ve somewhat achieved with the below code.

    <?php
    	$catList = explode('<br />', wp_list_categories('echo=0&show_count=0&title_li=&style=none'));
    	foreach ($catList as $cat)
    	{
    		$iPos = strpos($cat, '>');
    		if (is_category()) {
    			$addclass = ' class="current_page"';
    			} else {
    			$addclass = '';}
    			// add the opening span tag behind the opening anchor tag
    			$strLink = "<li" . $addclass . ">" . substr($cat, 0, $iPos) . '><span>' . substr($cat, $iPos + 1) . '</li>';
    	 		// add the closing span tag before the closing anchor tag
    	 		echo str_replace('</a>', '</span></a>', $strLink);
    	}
    ?>

    The problem is that when I click on a category it shows all of the category tags highlights and not just the selected one. I also noticed while viewing the source that it added in an extra

    • ><span>
    • at the end, but haven’t been able to figure out why.

      Any help would be greatly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter tecil

    (@tecil)

    I changed the coding around a bit to add a Home tab, but I’m still getting the same results.

    <?php
    	$catList = explode('<br />', wp_list_categories('echo=0&show_count=0&title_li=&style=none'));
    	foreach ($catList as $cat)
    	{
    		$iPos = strpos($cat, '>');
    		if (is_home()) {
    		$addclass = ' class="current_page"';
    		} else {
    		$addclass = '';
    		}
    		echo "<li" . $addclass . "><a href='" . get_option('home') . "' title='Home'><span>Home</span></a></li>";
    		elseif (is_category()) {
    			$addclass = ' class="current_page"';
    		} else {
    			$addclass = '';}
    			echo "<li" . $addclass . ">" . substr($cat, 0, $iPos) . '><span>' . substr($cat, $iPos + 1) . "</span></li>";
    	}
    
    ?>

    The tab for Home works great. On the main page all of the category tabs show up correctly, but if I click on a category all of the category tabs become highlighted instead of just limiting it to the clicked one. Plus I’m still getting this extra snipet of code that shows up at the end every time (even on the main page).

    <li class="current_page">><span></span></li>
    </ul>

    I think this is because the string WP returns has a
    at the end giving you one empty array element.

    Make this the second line after generating the array –

    array_pop($catList);

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Category Navbar Highlighting’ is closed to new replies.