Create child resources list with pdoMenu

Hi.
I am trying to create a universal template for my website. On pages where there are child resources, I want to display a list of these resources with a title. The closest option I could find is this:

  <h2>Title</h2>

  <div class="pagelist">
  [[pdoMenu? 
    &parents=`[[*id]]`
    &level=`1`
    &outerClass=`list`
    &levelClass=`level`
    &tplOuter=`@INLINE <ol[[+classes]]>[[+wrapper]]</ol>`
  ]]
</div>

But if I add it to the main template of the website, the title will be displayed on every page. Is it possible to make the title appear only if the page has child resources? Thank you.

Hi @Crewdk2023 …

You could try something like the following:

Create a snippet called hasChildren:

<?php
$document = $modx->getObject('modResource', $id);
        
if($document){
   $c = $modx->newQuery('modResource');
   $c->where(array(
       'parent' => $document->get('id'),
       'published' => '1',
       'hidemenu' => '0',
       'deleted' => '0'
   ));
   $has_children = $modx->getCount('modResource',$c);           
   return $has_children ? 'hasChildren' : 'noChildren';
}

Then in your original code:

[[!hasChildren:is=`hasChildren`:then=`

      <h2>Title</h2>

      <div class="pagelist">
      [[pdoMenu? 
        &parents=`[[*id]]`
        &level=`1`
        &outerClass=`list`
        &levelClass=`level`
        &tplOuter=`@INLINE <ol[[+classes]]>[[+wrapper]]</ol>`
      ]]
      </div>

`:else=`

        [[- your code if no children present ]]

`? &id=`[[*id]]`]]

Note that this will check if the current resource has children. It will only count published, unhidden and undeleted resources as children.

Someone else may have other solutions to this

You could also use the pdoResources snippet where you can put the title in the tplWrapper and use wrapIfEmpty when there are no results: pdoResources / Snippets / pdoTools / Extras / docs.modx.pro

Not sure if that’s the best way to do it but that way you don’t have to write custom code

1 Like

Thank you for reply. Is there any way to exclude some parents ID from generation?

You can exclude individual resources from your pdoMenu call like this:

[[pdoMenu? 
        &parents=`[[*id]]`
        &resources=`-10,-20,-30`
        &level=`1`
        &outerClass=`list`
        &levelClass=`level`
        &tplOuter=`@INLINE <ol[[+classes]]>[[+wrapper]]</ol>`
]]

This would exclude resources with IDs 10, 20 and 30 from the pdoMenu call results.

Does that help?

Thank you! This works perfect!

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.