We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20358
    • 10 Posts
    Hi Everyone,

    I’m trying to figure out a good way to apply a different link class in my menu for the current/active page as a way of making it easier on page viewers of knowing where they are. I’ve stumbled on several similar requests and people are often suggesting Wayfinder, but this seems to be overly complicated for the menu I’m trying to implement. Additionally, the class attributes in Wayfinder apply to the "li" tag and not the "a" tag, which is where I desire to make the class changes. My menu is an extremely simple, top-level (no submenus), horizontal list of 6 links. It’s also quite static, which is another reason I believe Wayfinder might be overkill.

    I’m applying a class called "hover1" to all links and wish to apply a different class, "hover2" to the active page.

    Here’s what the HTML looks like:

    
    <div id="navigations">
          <ul id="nav">
            <li><a class="hover1" href="#">Home</a></li>
            <li><a class="hover1" href="#">Cartoons</a></li>
            <li><a class="hover1" href="#">Videos</a></li>
            <li><a class="hover1" href="#">Rants</a></li>
            <li><a class="hover1" href="#">Archives</a></li>
            <li><a class="hover1" href="#">About</a></li>
          </ul>
    </div> 
    
    



    Here’s the CSS for "hover1" and "hover2":

    
    a.hover1:link, a.hover1:visited, a.hover1:active {color:black; text-decoration:none; }
    a.hover1:hover {color:red; text-decoration:underline; }
    
    a.hover2:link, a.hover2:visited, a.hover2:active {color:red; text-decoration:none; }
    a.hover2:hover {color:red; text-decoration:underline; }
    
    



    And the CSS for the "nav" ul:

    
    #nav {margin:0; padding:0;}
    #nav li {list-style: none; display: inline;  padding: 40px;}
    
    



    I’ve accomplished this the live, pre-CMS, version of my site using a rather messy PHP hack, but I’d like to avoid having to put anything other than "content" in these particular resources. Any guidance would be greatly appreciated.

    Thanks!

    Swill
      • 20358
      • 10 Posts
      Oh! And a quick explanation of "why" I desire to apply the class to the "a" instead of the "li": I have a Wordpress Loop plugged into the live version of this site, and to avoid having to hack through the WP code to update the link properties, I added a global CSS attribute that defaults all links to that "hover1" class unless otherwise specified. Because of this, applying dynamic CSS to the "li" in my menu does no good, because it gets overridden by the global "a" class (since the "a" gets called *after* the "li" in the markup). Hopefully that makes sense.

      Swill
        • 1169
        • 312 Posts
        I think I have a way for you.
        Create a TV for your template navigation
        Copy your first Code Post into TV as default
        Remove code from your template and replace with TV navigation
        Edit each resource for the active page to hover2.

        Susan always suggests TV’s and she should know.
          DEVELOPMENT ENV:- Ubuntu 12.04 | MODx Revolution 2.2.8 | LAMP 2i Apache 2.2.22 | Php 5.3.10 | Mysql 5.5.31 MySQL client version: 5.5.31
          • 32316
          • 387 Posts
          just because the li gets the class doesn’t mean that you can’t target the a with the css:

          html (possibly generated by wayfinder)
          <li class="active"> <a class="hover1" href="#">About</a></li> 

          css:
          .active a { text:blink;  } 


          blink?!? http://en.wikipedia.org/wiki/Blink_element

            • 36404
            • 307 Posts
            hi,

            furthermore, you can easily "ask" wayfinder to apply the class to the a and/or the li just adding its class placeholder where you want.
            <a href="[[+wf.link]]" title="[[+wf.title]]" [[+wf.classes]]>[[+wf.linktext]]</a>

            as simple as this... (if in Evo just use [+placeholder+] instead of [[+placeholder]])

            Have swing
              réfléchir avant d'agir
              • 33968
              • 863 Posts
              people are often suggesting Wayfinder, but this seems to be overly complicated for the menu I’m trying to implement
              I would have thought Wayfinder would be the simplest way to achieve this, rather than taking the trouble to create a custom snippet.

              virtualgadjo’s suggestion is a good one and easy to implement with wayfinder. Just call it like this:
              [[!Wayfinder? &startId=`0` &rowTpl=`rowTpl` &rowClass=`hover1` &hereClass=`hover2` ]]
              


              Then create a chunk called ’rowTpl’:
              <li><a href="[[+wf.link]]" title="[[+wf.title]]" [[+wf.classes]]>[[+wf.linktext]]</a></li>
              


              And it’s done! Wayfinder will throw in your hover classes for you. Much easier than hacking through Wordpress tongue
                • 36404
                • 307 Posts
                hi again,

                just one more little trick for you to see how nice is way finder smiley
                as you apparently already have a class for your link, you can wayfinder’s ones this way
                <a href="[[+wf.link]]" title="[[+wf.title]]" class="hover1 [[+wf.classnames]]">[[+wf.linktext]]</a>

                this to avoid a duplicate class=""
                <li [[+wf.classes]]><a href="[[+wf.link]]" title="[[+wf.title]]" class="hover1 [[+wf.classnames]]">[[+wf.linktext]]</a><li>

                more again, as wayfinder is not cheap... you may use its classes both on the li and the a !
                hope all this will convice you how easy and powerful wayfinder is, it’s one of the really great features of MODx don’t miss it...

                have swing
                  réfléchir avant d'agir
                  • 20358
                  • 10 Posts
                  Alright, you guys win smiley I just got virtualgadjo’s suggestion working in like 3 minutes... after spending quite a bit longer than that trying to get my alternate hack working (which still failed) grin

                  Thanks for the tips!

                  Swill