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

    Basically all I want to do is get an array of all the pages on the site and then run through a for loop to create an ordered list where I can change the class based on the current page id.

    Sounds simple, but I’ve been looking through the documentation and can’t figure out an easy way to grab the list of pages. I can get the documentID no problem. Is there an easy way to do this that I’m missing?

    Normally if I was writing something for myself I would just query the DB with something like (Select * From Pages Where Visible=true) which would return an array of active pages. Does MODx have anything like this?

    I’d appreciate the help! Thanks,

    Drezden

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Wayfinder. I strongly recommend spending some time with the docs you can download from this page.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 8039
        • 4 Posts
        I figured it out, I still need to play with things a little to make it do exactly what I want but this will do the trick for now. It will let me loop through all the pages in the site and change the class if I’m on that page.

        <?php
        $pages = $modx->getDocumentChildren($id=0, $active=1, $deleted='0', $fields='id, pagetitle, published,type,hidemenu');
        $currentId = $modx->documentIdentifier;
        echo "<ul id='navlist'>";
        foreach($pages as $page)
        {
             if($page["type"] == "document" && $page["published"] == "1" && $page["hidemenu"] == "0")
             {
                  echo "<li><a href='[~" . $page['id'] . "~]'";
                  if($page["id"] == $currentId)
                  {
                       echo " class='activeNav'>";
                  }
                  else
                  {
                      echo ">";
                  }
                  echo $page['pagetitle'] . "</a></li>";
             }
        }
        echo "</ul>";
        ?>
        
          • 27376
          • 576 Posts
          Quote from: drezden at Nov 14, 2007, 12:40 PM

          I figured it out, I still need to play with things a little to make it do exactly what I want but this will do the trick for now. It will let me loop through all the pages in the site and change the class if I’m on that page.
          Wayfinder does all this and more right out-of-the-box. If you want to save on programming time, I strongly recommend looking at the documentation.

          Wayfinder Parameters
          Wayfinder Examples

          Wayfinder is included with MODx 0.9.5 and up. Wayfinder 2.0 is worth the upgrade. But I understand the familiarity of hand-written code, so the offer is there. As well as support if you have questions about Wayfinder smiley

          Enjoy.
            • 8039
            • 4 Posts
            Thanks,

            Ultimately I’ll end up using Wayfinder, but I still like to know how to access things in the database and make changes with my own code. It gives me a better understanding of the MODx inner-workings because there isn’t always a nice plugin for every problem.

            Drezden

            laugh