We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20751
    • 122 Posts
    Trying to get the alias of a child’s parent. I create a snippet called parentalias with the following:

    <?php
    return $modx->getParent($id,1,'alias');
    ?>


    In my html i have:

    The alias is: [[parentalias]]


    The result I get is:

    The alias is: Array


    I’ve tried:


    • calling the snippet uncached
    • adding $modx->getParent($id,1,’alias’) to a variable in the snippet and then returning the variable
    • using a specific id instead of $id
    • testing with various child documents

    Any ideas as to where I’m going wrong?
      • 10487 MODX Staff
      • 1,535 Posts
      Try this:
      <?php
      $field = $modx->getParent($id,1,'alias');
      return $field['alias'];
      ?>
      
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 20751
        • 122 Posts
        Nope, that didn’t work either. Now instead of Array it says nothing. Does it matter that the document I’m using the snippet belongs to a document group?

          • 20751
          • 122 Posts
          Well bang goes that theory! I tried using the snippet in a public document and still the same problem. I just don’t get what I’m doing wrong!
            • 20751
            • 122 Posts
            Well using your suggestion has suddenly started working. Could someone explain why when using the method in the documentation doesn’t work but using the above method does? I’d really like to understand the difference.

            Many thanks!
              • 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.]
                • 3749
                • 24,544 Posts
                There's a more generic approach here: for getting any field of the current resource's parent (though that may not be what you want).

                The reason your original code didn't work is that getParent() returns an array of fields, even if you only ask for one.
                  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