We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21560
    • 145 Posts
    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.
      [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
      • 27376
      • 576 Posts
      This might just be my crazy in-the-box thinking, but I would call this "currentPage" seeing that in your scenario, it’s an on-going thing and there truly is no "last" chapter (yet).

      But this is an excellent addition and very useful! Thanks for sharing.
        • 21560
        • 145 Posts
        A very valid point. I’ll rename it to ’current’ wink

        I’m actually surprised there haven’t been more replies on this, as I’ve seen a few threads lying around here on the forums, but none of them have seem to found the (simple) solution I used...
          [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Quote from: Nimja at Dec 21, 2007, 08:18 AM

          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.
          Actually that’s not necessarily true; Wayfinder for example will generate a direct link to the desired page if the content of the weblink is a full URL. It only calls the sendRedirect function if the content is a document ID.

          http://www.sottwell.com/article-weblinks.html
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 21560
            • 145 Posts
            But the $this->sendRedirect() function compiles it properly, for outside/inside links, doesn’t it?

            It’s just that WayFinder scans ’WebLinks’ on their destination to have 1 hop instead of 2.
              [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com