We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Some of the resources on a site I’m working at now have children, and I wanted a simple nav for that in the sidebar. I didn’t feel like using getResources for that, so ended up writing a couple of xPDO to show how that can be achieved without the overhead of a full blown snippet.

    <?php
    // First fetch all the children of the current resource
      $children = $modx->resource->getMany('Children');
      // Check if there are any. If not, return false
      if (!$children) { return false; }
      // Check if there is a tpl set, if not return an error
      $tpl = $modx->getOption('tpl',$scriptProperties,'');
      if (!$tpl) { return 'No template given.'; }
      
      // Start the output
      $o = '<h2>[[*pagetitle]]</h2><ul>'; 
      // Look through the results...
      foreach ($children as $child) {
        // ... fetching the needed info...
        $out = array(
          'id' => $child->get('id'),
          'pagetitle' => $child->get('pagetitle'),
          'menutitle' => $child->get('menutitle'),
          'longtitle' => $child->get('longtitle'));
        // ... and adding it to the output as placeholders in the chunk
        $o .= $modx->getChunk($tpl,$out);
      }
      // Don't forget to close the list
      $o .= '</ul>';
    
      // Return the output
      return $o;


    It’s commented, so should be clear.

    I named the snippet getChildren, and call it in my template like this:
    [[getChildren? &tpl=`getChildrenTpl`]]


    The getChildrenTpl is a chunk which looks like this:
    <li><a href="[[~[[+id]]]]" title="[[+longtitle:default=`[[+pagetitle]]`]]">[[+menutitle:default=`[[+pagetitle]]`]]</a></li>



    I hope this snippet may shed some light on how to work with xPDO for some people smiley


    If I can find a place to put it in the RTFM I’d put it there.. but can’t find a suitable place right now. Open to suggestions! smiley


    This snippet could be better improved by setting an outer template which adds the header and the ul tags, but for what I needed it was fine like this.
      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
      • 3749
      • 24,544 Posts
      Nice example. cool

      I think the last line could use some editing -- did you paste from Word? wink
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
      • Oops!


        Pasted it in from Codemirror, but didn’t notice it had added all that stuff tongue

        I’m going a little more in depth about this on my Blog: http://www.markhamstra.nl/modx/general/tips-tricks-tutorials/get-children-snippet-and-learn-xpdo.html
          Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

          Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
          • 34017
          • 898 Posts
          Do you know how can you say getChildren to depth(3). Like getChildren and getGrandchildren in one query? or do you have to do multiple queries?
            Chuck the Trukk
            ProWebscape.com :: Nashville-WebDesign.com
            - - - - - - - -
            What are TV's? Here's some info below.
            http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
            http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
            • 3749
            • 24,544 Posts
            Easy as Pie:

            $modx->getChildIds($id, $depth);
            wink
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
            • I just encountered an issue with this simple example, it doesn’t check the hidemenu field so it displays everything, and not those which are checked to display in the menu :p

              Could just check for that in the foreach loop, and do nothing if it aint set.
                Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                • 3749
                • 24,544 Posts
                Quote from: Mark at Feb 24, 2011, 11:44 PM

                I just encountered an issue with this simple example, it doesn't check the hidemenu field so it displays everything, and not those which are checked to display in the menu :p

                Could just check for that in the foreach loop, and do nothing if it aint set.

                Right. I think getChildIds() will get all children including those that are not published or hidden from menus. I believe it will also get deleted children if the tree hasn't been purged.

                As Mark H., says, though, you're going to need a foreach loop to display them anyway and it's easy enough to check the hidemenu, published, and deleted fields before adding them to the output.

                If you use $resource->getMany('Children'), you can filter them in the call, but then you don't get to set the depth:

                <?php
                $resource->getMany('Children', array('hidemenu'=>'0', 'published'=>'1', 'deleted'=>'0'));
                [ed. note: BobRay last edited this post 8 years, 5 months ago.]
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                • Shouldn’t that be getMany()?
                    Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                    Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                    • 3749
                    • 24,544 Posts
                    Quote from: Mark at Feb 25, 2011, 11:19 AM

                    Shouldn’t that be getMany()?
                    Yes, it should. embarrassed
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 51397
                      • 2 Posts
                      Hi guys,

                      Hope you can help me with this:

                      QUESTION 1 – Sort by "publishedOn"
                      -----
                      I've been implementing this feature (Mark Hamster, Reply #1 in this thread) with success, but need to sort by "publishedOn".

                      I already tried to insert
                      &sortby=`{"publishedon":"DESC"} 
                      in my template with no luck.

                      Looks like this
                      [[press? &tpl=`tplBlog` &sortby=`{"publishedon":"DESC"}`]]


                      Whats wrong?

                      QUESTION 2 – How to output "publishedOn" for each children
                      -----

                      In addition – when I output "publishedon" at my frontend with this in my chunk:
                      <p>[[*publishedon]] - [[+content]]</p>
                      ...

                      ...all my children prints the "publishedon" for the parent, which is the same date for all children. How do I output the "publishedon"-date for each child?



                      My code is here:


                      SNIPPET - "press"
                      ---
                      <?php
                      // First fetch all the children of the current resource
                        $children = $modx->resource->getMany('Children');
                        // Check if there are any. If not, return false
                        if (!$children) { return false; }
                        // Check if there is a tpl set, if not return an error
                        $tpl = $modx->getOption('tpl',$scriptProperties,'');
                        if (!$tpl) { return 'No template given.'; }
                         
                        // Start the output
                        $o = '<h2>[[*pagetitle]]</h2><ul>'; 
                        // Look through the results...
                        foreach ($children as $child) {
                          // ... fetching the needed info...
                          $out = array(
                            'id' => $child->get('id'),
                            'pagetitle' => $child->get('pagetitle'),
                            'menutitle' => $child->get('menutitle'),
                            'content' => $child->get('content'),
                            'longtitle' => $child->get('longtitle'));
                          // ... and adding it to the output as placeholders in the chunk
                          $o .= $modx->getChunk($tpl,$out);
                        }
                        // Don't forget to close the list
                        $o .= '</ul>';
                       
                        // Return the output
                        return $o;



                      CHUNK - tplBlog
                      ---
                      <p>[[+content]]</p>
                      <p>[[*publishedon]]</p>



                      TEMPLATE - press
                      ---
                      [[press? &tpl=`tplBlog` &sortby=`{"publishedon":"DESC"}`]]