• Resolved martinhult

    (@martinhult)


    Hi,

    I’m wondering is there is any easy way to enable my current navigation menu to change color when a site is active. For example: when the site loads the first “Blogg” menu item should have a different color than the rest of the site and when I click on “Om mig” the color should change to that menu item.

    I’ve checked around the web for a solution but couldn’t find any that made sense to me so a rather basic step-by-step would be greatly appreciated.

    Kind regards,

    M.

    My site: https://www.martinhult.com

Viewing 2 replies - 1 through 2 (of 2 total)
  • The active link changes color for me (using Firefox).

    If your problem has been solved, please use the dropdown on the right to mark this topic ‘Resolved’ so that anyone else with this question can see that there is a solution.

    If it has not been solved, please try a different browser to see if that makes a difference.

    Thread Starter martinhult

    (@martinhult)

    What I did was to use WordPress own codes on the matter:

    https://codex.www.ads-software.com/Dynamic_Menu_Highlighting

    In particular ‘Method Two: With CSS in One Document’. The guide didn’t really help me at first as I didn’t know how to bridge the gap between WordPress codex and my own meny.

    The crux, as always, is in finding out and identifying your Site ID.

    To see what your pages IDs are you do the following:
    1.) Enter the admin mode through /wp-admin
    2.) Go to Pages
    3.) Hoved the Edit text and look down at the status bar of your browser. A number should be displayed in the site and the code should look something like this:

    https://www.martinhult.com/wp-admin/post.php?post=274&action=edit

    The number you are looking for is the one after post= which in my case is ‘274’.

    Going back to the code provided to us by WordPress in the codex it’ll look like this in the end:

    <ul id="menu">
    
            <!-- To show "current" on the home page -->
            <li<?php
                    if (is_home())
                    {
                    echo " id=\"current\"";
                    }?>>
                    <a href="<?php bloginfo('url') ?>">Home</a>
            </li>
            <!-- To show "current" on the About Page -->
            <li<?php
                    if (is_page('<strong>About</strong>'))
                    {
                    echo " id=\"current\"";
                    }?>>
                    <a href="<?php bloginfo('url') ?>/about">About</a>
            </li>
    </ul>

    You usually dont have to edit the is_home code as it will always bring you to the front page of the site (ie. index.php) which it considers home.

    The one thing you NEED to edit however is the section in the code that I made bold. The text there should be replaced with whichever number you managed to come by in your wp-admin trip.

    So MY version of the code would look something like:

    <ul id="menu">
    
            <!-- To show "current" on the home page -->
            <li<?php
                    if (is_home())
                    {
                    echo " id=\"current\"";
                    }?>>
                    <a href="<?php bloginfo('url') ?>">Home</a>
            </li>
            <!-- To show "current" on the About Page -->
            <li<?php
                    if (is_page('<strong>471</strong>'))
                    {
                    echo " id=\"current\"";
                    }?>>
                    <a href="<?php bloginfo('url') ?>/about">About</a> <!-- Remember to change this part to the one you want it to display. You can call it whatever as well as the url is a valid link. -->
            </li>
    </ul>

    And that’s it.

    Just dont forget to add a

    <style>
    #current
    {
    background-color: #336699;
    }
    </style>

    before you finish coding.

    Hope this will help someone struggling with adding dynamic navigation to their site.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Is there a easy way to make the active navigation link change color?’ is closed to new replies.