Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Khemraj Thapa
Khemraj Thapa
8,885 Points

adding active class to the menu, help please...

Can anyone help me with this please as I am following the theme development on the wordpress. Everything is alright so far but the active class doesn't seems to work on the menu bar but when I hover over it, its seems to be working. Anyone?? Thanks

9 Answers

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

That's actually correct and what we're looking for. The best way to approach this is to use WordPress's default CSS classes that it generates

We simplified our menu a lot for this project and didn't get into this in depth, but this is what you should work off of.

Sorry for the confusion!

PS - GREAT reskinning of the project site!

Khemraj Thapa
Khemraj Thapa
8,885 Points

hmm i managed to do it using .current-menu-item on the css but still not satisfied by directly hard-coding it. I am sure there are more dynamic way to do it. Anyway thanks and really enjoying your video lessons. :D

Hi, I added the class " .current-menu-item " and I still don't get any results. I would like to change the color of the navigation link based on if it's the current page or not. I.e. If I am at the Bio page the Bio link would be highlighted green.

My url/website

Gautam Thapar
Gautam Thapar
5,265 Points

Make sure active comes after hover style otherwise it won't be effective.

Khemraj Thapa
Khemraj Thapa
8,885 Points

didnt knew about that, anyway i tried it but it didnt work out..

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

In the source code, does the class show up in the right place? If so then it is likely a weird CSS hover behavior.

Khemraj Thapa
Khemraj Thapa
8,885 Points

Hi Zac, I couldn't see the active class in the source code. I am following your tutorials and implementing it on my own website. The css which I have used for the menu nav are
#topnav ul a:active, #topnav ul a:hover{background:red; }

Its just the example, how I have used but, only the hover style seems to work but not the active one. I have googled it and found out the wordpress add .current-menu-item on the active menu items, may be thats the reason for active class not working? Is there any solution for it?? I hope I am not confusing you. Thanks

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hey Khemraj Thapa no worries! We'll work this out :)

Let me know what stage you're working on so I can pull up the right project files to compare with yours.

Khemraj Thapa
Khemraj Thapa
8,885 Points

Hi Zac Gordon I am at the last stage and now waiting for launching it. Everything is working fine except the active class. Do you have any advice for making this work? Thanks

Zac Gordon
Zac Gordon
Treehouse Guest Teacher

And you're not noticing any difference in the source code between what is in the project files?

Khemraj Thapa
Khemraj Thapa
8,885 Points

Hi Zac Gordon I have just managed to upload the website online, do u mind checking it and see if u can figure it out. The link for the website is http://iamkhem.com. Thanks

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hmmm, can you post up your header.php file? I think we're gonna try doing a little markup modification.

Use a

Before your code and then after

To have it show up well in the forums :)

Khemraj Thapa
Khemraj Thapa
8,885 Points

Hi Zac, as you said, I have posted my header.php..

   <!DOCTYPE html>
   <html>
<head>
    <title>
        <?php 
            wp_title( '-', true, 'right');
            bloginfo('name');
        ?>
    </title>
    <meta name="viewport" content="width=device-width">
    <?php wp_head(); ?>
</head>
<body>

<div class="header-container">
        <header class="wrapper clearfix"> 
            <header id="topnav">
                <a href="/"><div class="icon-logo logo"></div></a>
                <a href="#" id="navbtn"><span>menu</span></a>
                <nav>
                    <ul>
                    <?php 
                    $args = array(
                        'menu' => 'main-menu',
                        'echo' => false
                        );
                    echo strip_tags(wp_nav_menu( $args ), '<a>');
                    ?>
                    </ul>
                </nav>
            </header>  
       </header>
   </div>

"

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Try this and see if it works

<?php 

$args = array(
   'menu' => 'main-menu'
);

?>
Khemraj Thapa
Khemraj Thapa
8,885 Points

Hi Zac, that didnt workout either. When I checked in chrome inspector, it should that wordpress added some more of its classes to the menu, making it more complicated.

Gautam Thapar
Gautam Thapar
5,265 Points

It works!

Add the active class with a border and then click a link and hold on.

The short time between link clicked and released is the active state.