We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4018
    • 1,131 Posts
    Ok...by request I thought I’d post how I did a page categories snippet. The idea is pretty simple. You have a section or page that is separated into different categories. You want a menu to be displayed that will have a listing of the categories themselves and a link that will go to a page (or the same page) that will show links to pages within a specific category. So...how would you do this? Well, there’s probably a bunch of different ways...but this is just one simple way of doing it. Here’s how:

    1. First, we’ll need to create a Template Variable and use the Listbox (Multi-Select) Input Type. For the Input Options Values you need to use the following format:

    Category Name 1==1||Category Name 2==2||Category Name 3==3||Category Name 4==4||Category Name 5==5

    Obviously you substitute the ’Category Name’ with your own category names. You’ll basically assign this variable to the page that will be used for the category pages themselves. Then, when you create a new category page, you simply select which categories a specific page belongs to.

    2. Create a new folder that will be used to store and publish your category pages. Depending upon whether you need a root page for displaying the categories themselves, you may choose to either leave this folder unpublished or not. I choose to leave it unpublished since I don’t want the folder to show up in my main menu and decided to instead display the category menu and results on different pages of my site.

    3. Now we’ll create a snippet that will be used to display the categories we created in the template variable. The name of the snippet can be whatever you want:

    //ID of page for category links
    $pageid = 1;
    //Name of Template Variable used for categories
    $tmplvarname = "TmplCategories";
    //CSS Class Name
    $classname = "childrens";
    //CSS ID Name
    $idname = "childrens";
    
    $dbase = $modx->dbConfig['dbase'];
    
    $sql = "SELECT * FROM ".$dbase.".".$table_prefix."modx_site_tmplvars WHERE name='".$tmplvarname."'";
    $rs = $etomite->dbQuery($sql);
    $row_rs = mysql_fetch_assoc($rs);
    $elementsarr = explode("||",$row_rs["elements"]);
    
    $output = '<div'.($idname<>""?' id="'.$idname.'"':'').($classname<>""?' class="'.$classname.'"':'').'><ul>';
    
    foreach($elementsarr as $v){
       list($item,$itemvalue) = explode("==",$v);
       $output .= "<li><a href='[~".$pageid."~]?cat=".$itemvalue."'>".$item."</a></li>\n";
    }
    
    return $output."</ul></div>\n";
    


    All this snippet does is display the categories from your template variable using a list menu. The main variables of interest is $pageid, $tmplvarname, $idname, and $classname. You can choose an ID and Class name for styling the list menu via CSS. Then you give it the template variable name via $tmplvarname and the ID of the page you wish to link your category names to via $pageid. When a user clicks on a link, it’ll redirect to the page given by the $pageid and will use the category number to display the pages of that category.

    4. Now you need another snippet for displaying the page links for a given category:

    //Set to the default folder for category pages
    $pid = 3;
    
    // list order by specified field
    if(!isset($order) || $order == ''){ 
       $order='menuindex';
    }
    
    // return false if no category is set
    if(!isset($_GET['cat']) || $_GET['cat'] == ''){
       return false;
    }else{
       $cat = $_GET['cat'];
    }
    
    // get childrens, and show some info when no childrens founds
    $children = $etomite->getDocumentChildrenTVars($pid,array("PortfolioCats","id","pagetitle"),1,$order);
    if($children == false){
       return '<blockquote>No categories to show</blockquote>';
    }
    
    // close list in div (optional, you can change it, whatever)
    $menu = '<div id="childrens"><ul>';
    $childrenCount = count($children);
    
    // lists all childrens as links
    for($x=0; $x<$childrenCount; $x++) {
       $childarray = explode("||",$children[$x][0]["value"]);
       for($i=0; $i <= count($childarray); $i++){
          if($childarray[$i] == $cat){
             $menu .= "<li><a href='[~".$children[$x][1]["value"]."~]'$active>".$children[$x][2]["value"]."</a></li>";
          }
       }
    }
    
    return $menu."</ul></div>\n";
    


    Simply set the $pid to the ID of the folder that will store your category pages. This output another list menu showing the pages in the given category as selected by a $cat variable from your category menu. Of course, you can choose to have this menu on the same page as your category menu. If the $cat variable isn’t present in the URL, no output will be shown. You can name this snippet to whatever you want as well...just give it a name that makes sense to you.

    Granted, this isn’t quite finished and I haven’t had time to play with it that much...but it’s a good start. If you need the category menu displayed as a drop-down menu, it should be pretty easy to format it as such. The same applies to the category pages display as well. Definitely a variety of ways to do it...you’ll just have to play with it and get it the way you want it. smiley

    Hope this helps! L8R!

    Jeff
      Jeff Whitfield

      "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
      • 32963
      • 1,732 Posts
      Quote from: Bravado at Jul 05, 2005, 10:29 AM

      Simply set the $pid to the ID of the folder that will store your category pages.? This output another list menu showing the pages in the given category as selected by a $cat variable from your category menu.? Of course, you can choose to have this menu on the same page as your category menu.? If the $cat variable isn’t present in the URL, no output will be shown.? You can name this snippet to whatever you want as well...just give it a name that makes sense to you.

      Very nice work smiley

      One small addition... Using snippet properties the user don’t have to modify the snippet code at all. Just add the following line to the snippet properties text box and enter the value from the displayed text input:

      &pid=Parent id;string;1

      Now just comment out the line $pid=1; from inside the snippet and the parser will make the $pid parameter available inside the snippet.

        xWisdom
        www.xwisdomhtml.com
        The fear of the Lord is the beginning of wisdom:
        MODx Co-Founder - Create and do more with less.
        • 33337
        • 3,975 Posts
        Hi Bravado !

        Thanks for sharing nice piece of code wink

        Best regards,

        zi
          Zaigham R - MODX Professional | Skype | Email | Twitter

          Digging the interwebs for #MODX gems and bringing it to you. modx.link
          • 8287
          • 12 Posts
          Thank you for making this available.
          This is something I can really put to use.

          Dennis
            • 25663 MODX Staff
            • 12,272 Posts
            Thanks Jeff... care to share a link?
              Ryan Thrash, MODX Co-Founder
              Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
              • 4018
              • 1,131 Posts
              I’m assuming you mean a link to an example on how this works? smiley

              Quote from: rthrash at Jul 06, 2005, 07:46 AM

              Thanks Jeff... care to share a link?
                Jeff Whitfield

                "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
                • 25663 MODX Staff
                • 12,272 Posts
                yes... a link showing the technique in use. :p
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • 4018
                  • 1,131 Posts
                  Ok...here’s a quick demo. It’s not perfect, but it will at least give you an idea:

                  http://www.horizoninnovations.com/cattest.html

                  L8R! smiley

                  Jeff
                    Jeff Whitfield

                    "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
                    • 4673
                    • 577 Posts
                    I haven’t installed this yet but I was wondering if the categories can be pulled up dynamically?
                      Tangent-Warrior smiley
                      • 4018
                      • 1,131 Posts
                      What do you mean? The way I did it, the category names are simply pulled from the template variable. So...adding new ones is simply a matter of adding in another one to the template variable and updating the category pages themselves with the new category. Only drawback to this is that if you remove a category, you’ll have to go back into each category page and update them to reflect the removal of the category. I’m sure once newer versions of MODx come out, other implementations of this idea would be possible with a plugin or module. smiley

                      Quote from: Carsten at Jul 06, 2005, 09:40 PM

                      I haven’t installed this yet but I was wondering if the categories can be pulled up dynamically?
                        Jeff Whitfield

                        "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."

                      This discussion is closed to further replies. Keep calm and carry on.