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

    I’m currently rebuilding my website and im using getResources to list the first level of pages and would like to be able to add the number of children the page has under it. For example:
    A

    * Aberaeron ( 5 ),
    * Abergavenny ( 6 ),
    * Abergele ( 7 ),
    * Aberystwyth ( 11 ),
    * Amlwch ( 2 ),
    * Ammanford ( 1 )

    I can echo the pages out but does get resources have the ability to count and display the number of children ??

    Thanks
    • IMO, it would be better to write a short Snippet to count the children of a specific Resource and use that in your getResource tpl. Here’s an example let’s call getChildCount:
      <?php
      $count = 0;
      $parent = isset($parent) ? (integer) $parent : 0;
      if ($parent > 0) {
          $criteria = array(
              'parent' => $parent,
              'deleted' => false,
              'published' => true,
          );
          $count = $modx->getCount('modResource', $criteria);
      }
      return (string) $count;
      ?>

      And a sample tpl for getResources using getChildCount (NOTE: this would be 100% cacheable):
      <div class="post">
      <a name="[[+alias]]"></a>
      <h3><a href="[[~[[+id]]]]" title="[[+description:default=`[[+longtitle]]`:htmlent]]">[[+pagetitle]]</a></h3>
      [[+introtext]]
      <p class="clear post-footer align-right">
      <span class="children">Children ([[getChildCount? &parent=`[[+id]]`]])</span>
      <a href="[[~[[+id]]]]" class="readmore" title="[[+longtitle]]">Read more</a> 
      <span class="date">[[+publishedon:strtotime:date=`%b %e, %Y`]]</span>
      </p>
      </div>
        • 22840
        • 1,572 Posts
        Hi Jason,

        Thanks for the reply, however I cant seem to get this to work, ( i’ve had a day of things not working lol ), and I’m not very good with PHP.

        What I have is the getresource in the page content like this:
        <div class="ul-left">
        <h3 id="a">A</h3>
        <ul>
        [[getResources? &parents=`10` &showHidden=`1` &tpl=`list` &limit=`30` &depth=`0`]]
        </ul>
        </div>


        Then in the list tpl I have:
        <li><a href="[[~[[+id]]]]">[[+pagetitle]]</a> ([[getChildCount? &parent=`[[+id]]`]])</li>


        This showed (0) after every entry however there are some documents there for it to count but they are hidden from menu.

        Thanks for the help

        Paul
        • Sorry, I edited the snippet code in the OP; it had changed from $resourceId to $parent and forgot to change the check in the if statement to check $parent.
            • 22840
            • 1,572 Posts
            That’s fantastic Jason and works like a charm grin

            Thanks allot

            Paul
            • Jason, I just used this Snippet too, so thanks. Consider dropping it in Repo? tongue
                [sepiariver.com] (https://sepiariver.com/)
              • Just used this for a new site I'm building - works brilliantly. Great stuff.

                Thank you for sharing.
                  Helen Warner
                  Number one pixel!
                  Proud to be linked with MODX at Crimson Pixel
                  • 36632
                  • 202 Posts
                  Used it as a Manager tool so I don't have to manually count hundreds of children resources.
                    • 33401
                    • 13 Posts
                    @opengeek .. I used modx evo , is there a snippet for getChildCount ??
                      • 42906
                      • 1 Posts
                      Hello,
                      thank you for your snippet. I have done few changes (add hidemenu=>false, return integer value)and used this snippet. Please see it below.

                      $count = 0;
                      $parent = isset($parent) ? (integer) $parent : 0;
                      
                      if ($parent > 0) 
                        {
                        $criteria = array('parent' => $parent, 'deleted' => false, 'hidemenu' => false, 'published' => true);
                        $count = $modx->getCount('modResource', $criteria);
                        }
                      
                      return (integer) $count;
                      



                      When I use this snippet it work's and the number of children is showed on the page (via chunk).

                      I need to test it's return value. So when the given ID don't have any children at all a want to ECHO string 'NO, there are no children'. In case that it has at least one children I want to ECHO string 'YES'. This is just for example - in my case, when there is any children I will ECHO some HTML code with the children.

                      So I tried to do it but it is not working. Please see both ways that I was using. It is just about to test output value of the snippet and if the return values is GT (greater than) 0, then I want to ECHO some code. Can you help me please?

                      1)
                      [[   [[!getChildCount? &parent=`[[+id]]`]]:gt=`0`:then=`YES`:else=`NO, there are no children`   ]]
                      
                      2)
                      [[!getChildCount? &parent=`[[+id]]`:gt=`0`:then=`YES`:else=`NO, there are no children`   ]]
                      



                      Thank you and have a nice day