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.
Check [[UltimateParent]] snippet (shipped in default evo install) it might be useful for you.
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.
-
☆ 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
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.
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.]
try to use the snippet pdoField of the pdoTools - package or fastField, which is also included with pdoTools now.