$id = $modx->documentIdentifier;
Hi, I’m still just getting into MODx, and PHx has seemed pretty appealing as my PHP knowledge is pretty limited. I’m definitely guilty of doing the above (using ifelse to change header images for example) - I’d rather use a snippet but I’ve fallen at the first hurdle!
Like I said, I’m still finding my way with PHP, but I was trying to write a snippet that changes the header image depending on the page. I know I could use a TV, but that’s another thing a client has to worry about adding when they create a new document, which I’d like to avoid. So far I’ve been using:
$id = $modx->documentIdentifier;
...which works fine, but I’m having trouble retrieving other document variables (namely the alias and parent values). So for example, for my basic page template, I want the path to the image to be ’/assets/images/[*alias*].jpg’, unless the parent is 7 eg, in which case the image path will be different.
I know this is real basic stuff (and there are probably better ways of doing it!), but I’d appreciate some pointers. I’ve been looking through the wiki but can’t seem to find what I’m looking for, and the trial and error approach isn’t getting me very far either!
Thanks
Dan,
Thanks (again!) Kongondo. I think I’ll probably start a new post; I get the MODx->documentIdentifier, I just can’t find/work out how to do the same for retrieving the alias or parent for example. If I can understand that bit I think I might be able to cobble the rest of the snippet together!
Dan
$modx->documentObject['variable-name']
<?php $parent = $modx->documentObject['parent']; if ($parent=="0") { $banner = '<img src="/veru/assets/images/[*alias*].jpg" alt="" />'; } if ($parent=="6") { $banner = '<img src="/veru/assets/images/trade.jpg" alt="" />'; } if ($parent=="7") { $banner = '<img src="/veru/assets/images/news.jpg" alt="" />'; } if ($parent=="8") { $banner = '<img src="/veru/assets/images/press.jpg" alt="" />'; } if (($parent=="12") or ($parent=="13") or ($parent=="14")) { $banner = '<img src="/veru/assets/images/products.jpg" alt="" />'; } if (($parent=="15") or ($parent=="16") or ($parent=="17")) { $banner = '<img src="/veru/assets/images/recipes.jpg" alt="" />'; } return $banner; ?>
switch($modx->documentObject['parent']) { case 0: $img = '[*alias*]'; break; case 6: $img = 'trade.jpg'; break; case 7: $img = 'news.jpg'; break; case 8: $img = 'press.jpg'; break; case 12: case 13: case 14: $img = 'products.jpg'; break; case 15: case 16: case 17: $img = 'recipes.jpg'; break; default: $img = 'defulat image here'; break; } return '<img src="/veru/assets/images/'$img'" alt="" />';
or a simple switch() would be most appropriate
http://www.tizag.com/phpT/switch.php
switch($modx->documentObject['parent']) { case 0: $img = '[*alias*]'; break; case 6: $img = 'trade.jpg'; break; case 7: $img = 'news.jpg'; break; case 8: $img = 'press.jpg'; break; case 12: case 13: case 14: $img = 'products.jpg'; break; case 15: case 16: case 17: $img = 'recipes.jpg'; break; default: $img = 'defulat image here'; break; } return '<img src="/veru/assets/images/'$img'" alt="" />';