Here is how to make a dynamic weblink with an example to pass through to the last page in a parent (used on
http://nimja.com/current to send to the last current chapter)
Of course this could easily be modified into random pages, sequential pages, pages shown by date, etc etc.
Let’s get to it:
Make a new snippet: "lastPage":
<?php
//Parent HAS to be given, we can't do anything without a parent.
if (isset($parent)) {
// &sortBY, field to sort by, defaults to pub_date.
$sortBy = (!empty($sortBy) ) ? $sortBy : 'pub_date';
// &sortHow, sort direction, defaults to ASC(ending).
// Options are: ASC, DESC
$sortHow = (!empty($sortHow) ) ? $sortHow : 'DESC';
//Fields we use, Alias or ID
$field = 'alias';
//Use nice MODX function to get children.
$pages = $modx->getActiveChildren($parent, $sortBy, $sortHow, $field);
$goto = $pages[0][$field];
$this->sendRedirect($goto);
print_r ($pages);
} else {
return "Error: No parent given";
}
?>
Make a new normal document with the alias you want. (’current’ in my case). Title and such are not important here, but it might be nice for other places.
Set the template to blank!
The only content in the template is(change to any parent you want):
[!lastPage? &parent=`0`!]
As soon as someone now goes to
nimja.com/current the site will automatically send the visitor through to
nimja.com/chapter-99
With this simple example you can of course go much much further, with ingeneous dynamic links and menu items and such.
Have fun!
ps. This snippet has some ’hidden’ features. An example to always go to the first item in a menu:
[!lastPage? &parent=`0` &sortBy=`menu_index` &sortDir=`ASC` !]
As a sidenote, this ’snippet’ does exactly what a weblink does for you, with added configurability. A weblink is nothing more then a $this->sendRedirect() page. Of course, I find it slightly neater to do the actual code in a small snippet.