We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39583
    • 103 Posts
    I would like to get the title, content etc of direct children to a specific resource to neatly line them in <ul>'s and <li>'s. How can I achieve this?

    https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo

    The example on this page does almost exactly what I want, except that it is for the children of the current page. But I would like to get children from a specific resource.

    Any help to this or something similar would be great.

    Thanks!
    //André [ed. note: phacer last edited this post 14 years, 2 months ago.]
      --
      André
      • 4172
      • 5,888 Posts
      getResources or wayfinder, both can do that.

      If you need to create a navigation or something like that, you would prefer to use wayfinder.

      For other listings, getResources.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 39583
        • 103 Posts
        I've tried them but I can't figure out how to do it!
          --
          André
          • 4172
          • 5,888 Posts
          <ul>
          [[getResources? &parents=`[[*id]]` &tpl=`yourTplchunk`]]
          </ul>



          Or, if you prefer a custom-snippet, something like that should do it.

          Modified version of Marks one:

          <?php
          
          $tpl = $modx->getOption('tpl', $scriptProperties, false);
          $parent = $modx->getOption('parent', $scriptProperties, $modx->resource->get('id'));
          $o = $modx->getOption('default', $scriptProperties, '');
          
          // Check if there is a tpl set, if not return an error
          if (!$tpl) {
              return 'No template given.';
          }
          
          // First fetch all the children of any resource
          $classname = 'modResource';
          $c = $modx->newQuery($classname);
          $c->where(array('parent' => $parent));
          
          if ($children = $modx->getCollection($classname, $c)) {
              // 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;
          
          [ed. note: Bruno17 last edited this post 14 years, 2 months ago.]
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 39583
            • 103 Posts
            Thanks, that looks like what I want! smiley

            How can I call it? I have placed it in a snippet called "getEvents" and I have a chunk called "getChildrenTpl" like the one in the other example.

            Would this be correct if the parent resource have id 16:
            [[getEvents? &id='16' &tpl='getChildrenTpl']]


            When I do this it just empty. Same if I use:

            <ul>
            [[getResources? &parents=`[[*id]]` &tpl=`yourTplchunk`]]
            </ul>


            Do I need to do something specific to the resource to make it show? Now its published but hidden from the menu.
              --
              André
              • 4172
              • 5,888 Posts
              try:
              [[getEvents? &parent=`16` &tpl=`getChildrenTpl`]]


              or:
              <ul>
              [[getResources? &parents=`[[*id]]` &tpl=`yourTplchunk` &showHidden=`1`]]
              </ul>


                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 39583
                • 103 Posts
                [[getEvents? &parent=`16` &tpl=`getChildrenTpl`]]


                is still empty.

                <ul>
                [[getResources? &parents=`[[*id]]` &tpl=`yourTplchunk` &showHidden=`1`]]
                </ul>


                prints the full array of each resource from the resource named Home, the first one with id 1 like:

                Array
                (
                    [tpl] => 'getChildrenTpl'
                    [tplOdd] => 
                    [tplFirst] => 
                    [tplLast] => 
                    [sortby] => publishedon
                    [sortbyTV] => 
                    [sortbyTVType] => string
                    [sortbyAlias] => 
                    [sortbyEscaped] => 0
                    [sortdir] => DESC
                    [sortdirTV] => DESC
                    [limit] => 5
                    [offset] => 0
                    [tvFilters] => 
                    ...


                Its four arrays, one for each child resource under home. But nothing from resource 16 which is on the same level as Home (1). Any ideas?
                  --
                  André
                  • 4172
                  • 5,888 Posts
                  forgot:

                  $classname = 'modResource';


                  added it above.

                  if you want all the childs of 16 then:

                  <ul>
                  [[getResources? &parents=`16` &tpl=`yourTplchunk` &showHidden=`1` &limit=`0` &depth=`1`]]
                  </ul>


                  and you will need your own chunk for 'yourTplchunk'

                  see also: http://rtfm.modx.com/display/ADDON/getResources
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 39583
                    • 103 Posts
                    Now both of them work! Thank you very much for your help Bruno!!! =)
                      --
                      André