WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Highlight current taxonomy menu (2 posts)

  1. Archie22is
    Member
    Posted 1 year ago #

    Hi guys,

    I have deleted the default WordPress menu (no need for it on the site) and implemented a Category/Taxonomy menu. I am making use of the default WordPress categories to generate the menu.

    I have the following code:

    <?php
    	# Navigation Taxonomy Menu ---> so dynamic it's unbelievable!
    	$mylinks = get_terms('category', array(
    		'parent' => 0,				# display parent category links only
    		'orderby' => 'name',
    		'hide_empty' => 0
    		)
    	);
    	$count = count($mylinks);
    
    	if ( $count > 0 ) {				# From the above, we create a menu
    		foreach ( $mylinks as $mylink ) {
    			$i++;
    			echo '<li class="page_item-' . $i . '">'; 		// add incrementing value to id page
    			# get current webdirectory and link to taxonomy menus
    			echo '<a href=' . network_site_url() . '/' . $mylink->slug . ' class="'. $mylink->slug .'" >';
    			echo $mylink->name;
    			echo "</a>";
    			echo "</li>";
    			}
    		}
    ?>

    The menus are generated fine and work and group the categories as expected.

    My only problem is that I cannot highlight the current selected category using CSS. This is how the html code looks:

    <nav class="clearfix">
    	<ul class="menu clearfix">
    		<li class="page_item-1">
    			<a href=http://localhost/Traderzone/alcohol class="alcohol" >Alcohol</a>
    		</li>
    		<li class="page_item-2">
    			<a href=http://localhost/Traderzone/snacks class="snacks" >Snacks</a>
    		</li>
    		<li class="page_item-3">
    			<a href=http://localhost/Traderzone/steak class="steak" >Steak</a>
    		</li>
    	</ul>
    </nav>

    How will I get to intergrate a "active" id to add some css? jQuery or can it be done within php?

  2. Archie22is
    Member
    Posted 1 year ago #

    Hi guys,

    Again I am amazed by the power of WordPress. This is how I managed to solve the problem.... with just one line of code (AMAZED).

    wp_list_categories('orderby=name&title_li=&hide_empty=0&hierarchical=0&parent=0&depth=0');

    If WordPress was a woman, I would consider marrying it...

    And this is my css code:

    .current-cat{
    	background-color: #d86f08;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic