We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51274
    • 7 Posts
    I'm trying to output certain meta description depending on resource ID and parent ID using ModX Revo.

    The problem is that Modx filters only can operate only one special tag (or id, or parent id, or something else), for ex.

     [[*id:is=`331`:then=`<meta name="description" content="[[*description]] — Page [[+page]]" />`:else=`<meta name="description" content="[[*description]]" />`]] 


    In this case tag is ID. In my situation I need to add to this condition one more statement, it must work like:

    - if [[*id]] is 331 then...
    - else if [[*parent]] is 321 then..
    - else...

    How can I do this without creating templates or chunks?
    I tried some variants:

    This just doesn't work

    [[*id:is=`331`:then=`<meta name="description" content="[[*description]] — Page [[+page]]" />`:else=`[[*parent:is=`321`:then=`<meta name="description" content="[[*description]] - News Page" />`:else=`<meta name="description" content="[[*description]]" />`]]]] 


    OR

    This on the 331st resource outputs two descriptions

     [[*id:is=`331`:then=`<meta name="description" content="[[*description]] — Page [[+page]]" />`:else=``]] 
        [[*parent:is=`321`:then=`<meta name="description" content="[[*description]] - News Page" />`:else=`<meta name="description" content="[[*description]]" />`]]
      • 51274
      • 7 Posts
      OK, I tried to write a snippet. But it also doesn't work. Why???

      getDescription snippet:
      <?php
      $id = $resourceid;
      
      $output = '<meta name="description" content="[[*description]]" />';
      
      
      if($modx->getParent($id,1,'id') == 321){
      
       $output = '<meta name="description" content="[[*description]] — News Page" />';
      
      } else {
      
       if ($id == 331) {
            $output = '<meta name="description" content="[[*description]] — Page [[+page]]" />';
       } else {
            $output = '<meta name="description" content="[[*description]]" />';
       }
      
      }
      
      echo $output;
      
      return;


      And I call it in my template:
      [[!getDescription? &resourceid=`[[*id]]`]]


      Please help! I don't understand why it doesn't work((
        • 3749
        • 24,544 Posts
        Try this:

        <?php
        $docId = $modx->resource->get('id');
         
        $output = '<meta name="description" content="[[*description]]" />';
         
         
        if($modx->resource->get('parent') == 321){
         
            $output = '<meta name="description" content="[[*description]] — News Page" />';
         
        } elseif ($docId == 331) {
         
            $output = '<meta name="description" content="[[*description]] — Page [[+page]]" />';
        }
         
        return $output;
         

          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 51274
          • 7 Posts
          Quote from: BobRay at Oct 19, 2015, 10:34 PM
          Try this:

          <!--?php
          $docId = $modx--->resource->get('id');
           
          $output = '<meta name="description" content="[[*description]]">';
           
           
          if($modx->resource->get('parent') == 321){
           
              $output = '<meta name="description" content="[[*description]] — News Page">';
           
          } elseif ($docId == 331) {
           
              $output = '<meta name="description" content="[[*description]] — Page [[+page]]">';
          }
           
          return $output;
           


          It works! Thanks) Maybe you could say, why did not my code work?
            • 3749
            • 24,544 Posts
            You were very close. I think the main problem was here:

            $id = $resourceid;


            The $resourceid variable isn't set by MODX in Revolution and your code doesn't set it, so the $id variable wasn't set.

            I don't think getParent() exists in Revolution (I could be wrong), but even if it did, it's going the long way round since you already have the resource object and only want its 'parent' field, which contains the parent's ID.

            Your code might have worked in MODX Evolution.



              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting