We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37619
    • 79 Posts
    I'm using MODX 2.2.10.

    This works:
    // Get Current ID
    $page_id = $modx->resource->get('id');
    
    $page = $modx->getObject('modResource',$page_id);
    $uri = $page->get('uri');
    $output .= "<p>".$uri."</p>";
    
    // Returns page-uri.html


    This doesn't:
    // Get Current ID
    $current_id = $modx->resource->get('id');
    
    // Get Parent IDs (returned in an Array)
    $parent_ids = $modx->getParentIds($current_id);
    
    foreach($parent_ids as $key => $page_id){
        $page = $modx->getObject('modResource',$page_id);
        $uri = $page->get('uri');
        $output .= "<p>".$uri."</p>";
    }


    I get this error:
    Fatal error: Call to a member function get() on a non-object in /core/cache/includes/elements/modsnippet/35.include.cache.php on line 31

    How come?
      Jean-Marc Buytaert (@jmbuytaert)

      MODX truly is the greatest thing that's ever happened to the Internet.
      • 8386 ☆ A M B ☆
      • 160 Posts
      Try outputting $page_id in your second example, I can almost think this is blank.
        • 22303 MODX Staff
        • 10,725 Posts
        0 is a valid parent, but will not be an object.
          • 37619
          • 79 Posts
          Howdy!
          Thanks for the quick reply.

          $page_id returned both parent page ids.

          When I do:
          print_r($parent_ids);
          


          I get this:
          Array
          (
              [0] => 12
              [1] => 0
          )


          So... basically, I'm trying to get every parent IDs/alias/menutitle of a resource. How can I do that?
            Jean-Marc Buytaert (@jmbuytaert)

            MODX truly is the greatest thing that's ever happened to the Internet.
            • 8386 ☆ A M B ☆
            • 160 Posts
            0 (zero) wouldn't be a ID in the database, so you would need to substitute that for 1, although I am against hard coding values.
              • 37619
              • 79 Posts
              Quote from: easylancer at Dec 31, 2013, 11:31 AM
              0 (zero) wouldn't be a ID in the database, so you would need to substitute that for 1, although I am against hard coding values.

              That was it. To "fix" it, I did this:
              // Get Current ID
              $current_id = $modx->resource->get('id');
              
              // Get Parent IDs (returned in an Array)
              $parent_ids = $modx->getParentIds($current_id);
              
              foreach($parent_ids as $page_id){
                  if($page_id != 0){
                      $page = $modx->getObject('modResource',$page_id);
                      $uri = $page->get('uri');
                      $output .= "<p>".$uri."</p>";
                  }
              }


              Thank you! And Happy New Year!
                Jean-Marc Buytaert (@jmbuytaert)

                MODX truly is the greatest thing that's ever happened to the Internet.
                • 42602
                • 81 Posts
                I hope I can simplify this, I guess you are trying to get all parent objects. Simply do this all in one query.

                $c = $modx->newQuery('modResource');
                $c->where(array(
                   'id:IN' => $modx->getParentIds($modx->resource->id));
                ));
                
                $pages = $modx->getCollection('modResource', $c);
                foreach ($pages as $page) {
                   $output .= "<p>{$page->uri}</p>";
                }
                return $output;
                


                You can add $c->sortby('id', 'DESC'); which most likely is the order unless you have moved loads of resource around the tree