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

    Could anyone please kindly tell me how to get the document ID? Example as below:

    My Document Tree:
    ROOT >> MainMenu-01
    ROOT >> MainMenu-02
    ROOT >> MainMenu-03 >> SubMenu-03 >> LastMenu-03

    What I want to do is when I select "LastMenu-03" I would like to get the document ID of "MainMenu-03"
    I manage to get the parent ID of "LastMenu-03" i.e. "SubMenu-03" document ID but my coding (I tried do while loop) does not allow me to get the "MainMenu-03" document ID.

    Thanks and regards,
    Wayne.
      • 32013
      • 24 Posts
      Hi,

      I managed to solve the problem, here is the code:
      //<?php
      $selectedId = $modx->documentIdentifier; /* Check selected document id */
      $parentId = $modx->getParent($selectedId,1,'id');  /* Check if selected document has parent */
      
      if ($parentId == "") { // If selected document does not have parent or parent level = root
        echo "[[Wayfinder? &startId=`$selectedId` &level=`2`]]";
      } else {
        $currentParentId = $parentId[id];
        /* The while loop check if selected document has grandparents */
        while ($parentId[id] != "") {
          $parentId = $modx->getParent($parentId[id],1,'id');
          if ($parentId[id] != "") {
              $currentParentId = $parentId[id];
          }
        }
        echo "[[Wayfinder? &startId=`$currentParentId` &level=`2`]]";
      }
        • 33337
        • 3,975 Posts
        Check [[UltimateParent]] snippet (shipped in default evo install) it might be useful for you.
          Zaigham R - MODX Professional | Skype | Email | Twitter

          Digging the interwebs for #MODX gems and bringing it to you. modx.link
          • 32013
          • 24 Posts
          Hi Zi,

          Thanks for letting me know about the [[UltimateParent]], it’s definitely more efficient than writing so many lines of code. However, I am not sure why it does not work when I tried below several methods except the last method:

          1st Method - Not Working
          I directly include in the Template as [[Wayfinder? &startId=`[!UltimateParent!]`]]
          I echo [[UltimateParent]] and I can definitely obtain the parent (before ROOT level) of the document.

          2nd Method - created a snippet Not Working
          "subMenu" Snippet Code:
          echo "[[Wayfinder? &startId=`[!UltimateParent!]`]]";
          Call subMenu snippet in Template: [[subMenu]]

          3rd Method - created a snippet Not Working
          "subMenu" Snippet Code:
          $parentId = [[UltimateParent]];
          echo "[[Wayfinder? &startId=`$parentId`]]";
          Call subMenu snippet in Template: [[subMenu]]

          4th Method - created a snippet Working
          "subMenu" Snippet Code:
          $parentId = [[UltimateParent]];
          echo $parentId;
          echo "[[Wayfinder? &startId=`$parentId`]]";
          Call subMenu snippet in Template: [[subMenu]]
          But that will also print out the numeral of the parent ID.


          Much appreciate if you could let me know what’s wrong with my first 3 methods coding.

          Thanks and regards,
          Wayne.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            This has to do with how MODx handles cached and uncached snippets. If you nest snippets, the outer snippet must be uncached and the inner snippet cached.
            [!Wayfinder? &startId=`[[UltimateParent]]`!]


            MODx tags can’t be used in snippet code, PHP doesn’t understand them. Use
            $modx->runSnippet('UltimateParent');


            Your last method is (sort of) appearing to work because you actually echo the snippet tag, which MODx then parses.

            http://www.sottwell.com/how-snippets-work.html
              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
              • 32013
              • 24 Posts
              Hi Sottwell,

              Thank you so much for the reply. I tried it and it works perfectly fine. Also thanks for the URL and the explanation.

              Regards,
              Wayne.
                • 46404
                • 5 Posts
                snippet (parent_title)
                <?php
                // created by Badichan Dmitriy (c) badichan.com
                $page = $modx->getObject('modResource', $input);
                $id = $page->get('parent');
                $parent_page = $modx->getObject('modResource', $id);
                echo $parent_page->get('pagetitle');
                ?>
                chunk
                [[!parent_title? &input=`[[*parent]]`]] [ed. note: badichan last edited this post 12 years, 8 months ago.]
                  • 4172
                  • 5,888 Posts
                  try to use the snippet pdoField of the pdoTools - package or fastField, which is also included with pdoTools now.
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!