We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6659
    • 18 Posts
    I modified the FirstChildRedirect snippet and think this might be of use to someone else. I needed a way to redirect a user to the first child of a document if there is only one child. I’ve dubbed this "OnlyChildRedirect". Here’s your code:

    <?php
    /**
     * @name OnlyChildRedirect
     * @author Jason Coward <[email protected]>
     * @modified-by Ryan Thrash <[email protected]>
     * @modified-by Dave Child <[email protected]>
     * @license Public Domain
     * @version 1.0
     * 
     * This snippet redirects to the first child document of a folder in which this
     * snippet is included within the content (e.g. [!OnlyChildRedirect!]) if there 
     * is only one child present.  This allows MODx folders to emulate the 
     * behavior of real folders since MODx usually treats folders as actual 
     * documents with their own content.
     * 
     * Modified to make Doc ID a required parameter... now defaults to the current 
     * Page/Folder you call the snippet from.
     * 
     * &docid=`12` 
     * Use the docid parameter to have this snippet redirect to the
     * first child document of the specified document.
     */
    
    $docid = (isset($docid))? $docid: $modx->documentIdentifier;
    
    $children= $modx->getActiveChildren($docid, 'menuindex', 'ASC');
    if ((!$children === false) && (count($children) == 1)) {
        $firstChild= $children[0];
        $firstChildUrl= $modx->makeUrl($firstChild['id']);
        return $modx->sendRedirect($firstChildUrl);
    }
    return '';
    ?>
      • 7231
      • 4,205 Posts
      Cool, this is a nice addition. Thanks grin
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don&#39;t know what it is.
        Do you, Mr. Jones? - [bob dylan]