We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20014
    • 200 Posts
    I use a single template for several of my pages yet, I wanted certain content to be shown depending on the page id.

    in evo 1.0.4 this worked nicely (have changed the original chunk syntax for revo in following)
    <?php
    $doc_id = $modx->documentIdentifier;
    $doc_array_1 = array('33');
    $doc_array_2 = array('32');
    $doc_array_3 = array('4');
    $doc_array_4 = array('48');
    $doc_array_5 = array('6');
    $doc_array_6 = array('71');
    $doc_array_7 = array('57');
    $doc_array_8 = array('87');
    $doc_array_9 = array('88');
    $doc_array_10 = array('86');
    $doc_array_11 = array('101');
    $doc_array_12 = array('100');
    $doc_array_13 = array('110');
    $doc_array_14 = array('109');
    $doc_array_15 = array('120');
    $doc_array_16 = array('8');
    if  (in_array($doc_id, $doc_array_1))
      $output="[[$randomimagephotos1]]";
    elseif  (in_array($doc_id, $doc_array_2))
       $output="[[$randomimagephotos2]]";
    elseif (in_array($doc_id, $doc_array_3))
       $output="[[$randomimagephotos3]]";
    elseif (in_array($doc_id, $doc_array_4))
       $output="[[$randomimagephotos4]]";
    elseif (in_array($doc_id, $doc_array_5))
       $output="[[$randomimagephotos5]]";
    elseif (in_array($doc_id, $doc_array_6))
       $output="[[$randomimagephotos6]]";
    elseif (in_array($doc_id, $doc_array_7))
       $output="[[$randomimagephotos7]]";
    elseif (in_array($doc_id, $doc_array_8))
      $output="[[$randomimagephotos1]]";
    elseif (in_array($doc_id, $doc_array_9))
       $output="[[$randomimagephotos2]]";
    elseif (in_array($doc_id, $doc_array_10))
       $output="[[$randomimagephotos3]]";
    elseif (in_array($doc_id, $doc_array_11))
       $output="[[$randomimagephotos4]]";
    elseif (in_array($doc_id, $doc_array_12))
       $output="[[$randomimagephotos5]]";
    elseif (in_array($doc_id, $doc_array_13))
       $output="{{randomimagephotos6]]";
    elseif (in_array($doc_id, $doc_array_14))
       $output="[[$randomimagephotos7]]";
    elseif (in_array($doc_id, $doc_array_15))
       $output="{{randomimagephotos8]]";
    elseif (in_array($doc_id, $doc_array_16))
       $output="[[$randomimagephotos8]]";
    else
       $output="[[$randomimagephotos4]]";
    return "$output";
    ?>


    though it did work evo, I cant get it to work with revo, is the docomunet indentifier the same in revo? ie: $modx->documentIdentifier; ?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Hmm. While I can’t address the documentIdentifier question, I can say that I would do this entirely differently.
      $id = <however one determines the resource's ID>;
      
      switch ($id) {
          case '33':
              return "[[$randomimagephotos1]]";
          break;
          case '32':
              return "[[$randomimagephotos2]]";
          break;
      etc...
      etc...
      
          default:
              return "[[$randomimagephotos4]]";
      }
      return;
      
        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
        • 20014
        • 200 Posts
        thanks susan. will try and get back
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Hah! Took a bit of digging, but I found it
          $modx->resource->get('id')
            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
            • 20014
            • 200 Posts
            I can’t get that to work either :’( , so I am beginning to think that the document identifier has changed (?)
            I am googling like heck, and the document object listed are still for evo, and bob’s site explains why to use

            $modx->documentIdentifier;

            and not

            $modx->getDocumentIdentifier(’id’);

              • 20014
              • 200 Posts
              alrighty laugh!!!! I posted before I saw your reply...off to check!!
                • 20014
                • 200 Posts
                Didn’t function, I did this as a test:

                <?php
                $id = $modx->resource->get('id');
                switch ($id) {
                    case '33':
                        return "[[$randomimagephotos1]]";
                    break;
                    case '32':
                        return "[[$randomimagephotos2]]";
                    break;
                    case '4':
                        return "[[$randomimagephotos3]]";
                    break;
                    default:
                        return "[[$randomimagephotos4]]";
                }
                return;
                ?>


                and also added $modx->resource->get(’id’); to my old snippet to see if it worked, and it didn’t.
                perhaps I should explain what I was trying to do and how I did it in evo bear with me.

                I had a single template and I wanted a graphic particular to that page to be shown, but I also wanted that graphic to change with each visit.

                Now, getting the page to show something particular per id was easy, getting it to change images contained within a folder specific to that page was easy as well, combining the two was not.

                I had to create a TV (syntax changed to reflect revo now) [[*imagesubheader]]
                then in the default of that tv I placed a snippet [[imagesubheader]]
                the snippet contained the code above which returned chunks according to page id
                within the chunk was a div which, in turn, contained yet another snippet which randomized the images.
                here is the chunk
                <div class="content_photo" id="content_photo">[[randomimagephotos1]]</div>

                and the random snippet:

                <?php
                $total = "10";
                $file_type = ".jpg";
                $image_folder = "assets/templates/mysites_template/images/page1images";
                $start = "1";
                $random = mt_rand($start, $total);
                $image_name = $random . $file_type;
                echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";
                ?>


                it was a total pain in the behind, but it was the only way it worked, I had tried it in several ways but using the TV was the only solution which worked.
                so is there a syntax error, or am I causing a conflict with in revo that I could get away with in evolution? I am stumped...
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  So each resource has an associated group of images (in a folder, like a gallery, I presume?) and you want a random image selected from its associated images folder for each page. Is this what you are trying to do?
                    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
                    • 20014
                    • 200 Posts
                    yes, for example on the three main pages of the site there is a large image which reflects the contents of that page, it’s not a gallery exactly, but each time someone would go to my photography page for example, a new image would appear from a folder specific to that page.
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      that seems like a rather complicated way to go about it. What about a rather simple snippet that just gets the id, then gets the random image and returns it (or loads it into a placeholder)? Put the snippet in the template and it’s good to go for every page.
                        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