We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27
    • 117 Posts
    Hi all

    I was wondering if any of you clever people could suggest what I should do if I wanted to include an image with my ListRelated links.

    I have an image TV called ’photo’ for each of my pages, and I was hoping that something like:

    <a href="[~’ . $relArray[$li][’id’] . ’~]"><img src=" ’. $relArray[$li][’tvphoto’] .’ " /></a>

    might just do it, but im crap at code and it failed miserably :’(

    Any guidance would be gratefully recieved.

    Cheers

    0
      • 11975
      • 2,542 Posts
      Hi,

      I guess you can achieve it using $modx->getTemplateVar()

      Around line 136

      try adding this line in the for instruction

      $mytv = $modx->getTemplateVar('photo', $fields='*', $relArray[$li][id']);
      


      To print out the value of your tvphoto:

      $relatedOutput . =’<li><a href="[~’ . $relArray[$li][’id’] . ’~]">’.$mytv[’value’].’</a></li>’;

      I haven’t tested it but it’s almost the same code I used to make newsListings snippet with images.

      Hope this helps smiley

      EDIT: I guess that if you set an image widget to your TV the output contains the <img> tag


      RE-EDIT: I’ve tested but doesn’t work

      here is what getTemplateVar brings me back with print_r($myTv):

      Array (
      [id] => 4
      [type] => image
      [name] => picture
      [caption] => Vignette Produit
      [description] => Image de présentation
      [editor_type] => 0
      [category] => 0
      [locked] => 0
      [elements] =>
      [rank] => 0
      [display] => image
      [display_params] => &align=none
      [default_text] =>
      [value] =>
      )

      LAST- EDIT : Everything is ok ;-)
      I forgot to set a value to my TV for the related doc
      Don’t forget to check in the code if the value is not empty
        Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
        • 11975
        • 2,542 Posts
        here is the code with TV inside
        What I mean is that I had nothing on screen because the related stuff had no image so ... it couldn’t work


        // --------------------
        // Snippet: ListRelated
        // --------------------
        // Version: 1.0 beta 1
        // Date: September 6, 2005
        // [email protected]
        //
        // This snippet was designed to show a list of pages that are
        // related based on specified keywords, or those used in the
        // current page
        //
        // Based on ListRelated by Jaredc and updated for MODx.
        // [email protected]
        //
        // Needs to be updated to support Webuser Logins Menu Items
        // and also possibly "in menu" items.
        //
        
        // Configuration Settings
        
           // LR_keywords [ string ]
           // This is a comma separated list of keywords that can be set
           // from the snippet parameter ONLY. Ex:
           // [[ListRelated?LR_keywords=fun,functional,good stuff]]
           // If no snippet variable is set, related pages will be found
           // based on the host page's keywords.
        
           // $maxRelated [ int ]
           // The maximum number of related pages you want to have. Can also
           // be set/overridden as the snippet variable "LR_max" as in:
           // [[ListRelated?LR_max=5]]
           $maxRelated = 10;
        
           // $relatedListTitle [ string ]
           // Give your related list of links a title if you desire- otherwise
           // set as an empty string ''. Can also be declared in snippet call
           // with "LR_title" like:
           // [[ListRelated?LR_title=Other links you might like]]
           $relatedListTitle = 'Related links:';
        
           // $linkToSearch [ true | false ]
           // If you would like to have a link at the end of your list for more
           // links based on search results of the current keywords.
           // FUTURE FEATURE - NOT CURRENTLY USED
           $linkToSearch = false;
        
           // $removeNewlines [ true | false ]
           // As always, with lists, it's best NOT to have new lines and whitespace
           // with IE. For debugging and reading source code, set to false, for live
           // implementation and styling, try setting to true.
           $removeNewlines = true;
           
           // $sortBy [ 'date' | 'pagetitle' ]
           // Field you would like to sort by. Options include:
           // date       - dates will be sorted by pub_date if it exists, else createdon
           // pagetitle  - page title
           // Can also be set in snippet call with $LR_sortBy (see example under $sortDir)
           $sortBy = 'date';
           
           // $sortDir [ 'ASC' | 'DESC' ]
           // Choose to sort in ascending (ASC) or descending (DESC) order. Can be set
           // in snippet call with $LR_sortDir:
           // [[ListRelated?LR_sortBy=date&LR_sortDir=DESC]]
           $sortDir = 'DESC';
        
           // $nonefound [ 'string' ]
           // What to return when no related  records are found.
           $nonefound = '<p>No related pages or links.</p>';
        
        // Styles
        
           // .LR_listTitle     class for list title
           // .LR_linksList     class for ul element
        
        // ***********************************
        // END CONFIG SETTINGS
        // THE REST SHOULD TAKE CARE OF ITSELF
        // ***********************************
        
        // assign IE variable
        $newline = ($removeNewlines)? '':"\n";
        
        // assign appropriate default or passed variables
        $maxRelated = (isset($LR_max))? $LR_max : $maxRelated;
        $relatedListTitle = (isset($LR_title))? $LR_title : $relatedListTitle ;
        $sortBy = (isset($LR_sortBy))? $LR_sortBy: $sortBy ;
        $sortDir = (isset($LR_sortDir))? $LR_sortDir :$sortDir ;
        
        // Make array of keywords from snippet or from current page. Snippet
        // variables have priority.
        
        if (isset($LR_keywords)){
          $kArray= explode(",",$LR_keywords);
        } else {
          $kArray = $modx->getKeywords();
        }
        
        // if there are keywords, make the list
        $relatedOutput = '';
        if ($kArray) {
          $keyString = '';
          foreach ($kArray as $key){
            $keyString .= "'".$key."',";
          } 
          $keyString = substr($keyString,0,strlen($keyString)-1);
        
          // make sql statement for retreiving similar pages
          $pre= $modx->dbConfig['dbase'].".".$modx->dbConfig['table_prefix'];

        $relatedSql = "
        SELECT DISTINCT content.id, content.pagetitle,
        IF(content.pub_date > 0, content.pub_date, content.createdon) AS date
        FROM
        ".$pre."site_content AS content,
        ".$pre."keyword_xref AS xref,
        ".$pre."site_keywords AS keywords
        WHERE keywords.keyword IN (".$keyString.")
        AND xref.keyword_id = keywords.id
        AND content.id = xref.content_id
        AND content.id != ".$modx->documentObject['id']."
        ORDER BY ". $sortBy . ' ' . $sortDir ."
        LIMIT ".$maxRelated.";";

        $relatives = $modx->dbQuery($relatedSql);
        $relativeCount = $modx->recordCount($relatives);
        $relArray = array();
        for($i=0;$i<$relativeCount;$i++) {
        array_push($relArray,$modx->fetchRow($relatives));
        }

        // start output if we have results
        if ($relativeCount) {
        $relatedOutput .= ($relatedListTitle )?'<span class="LR_listTitle">' . $relatedListTitle . '</span>' : '' ;
        $relatedOutput .= '<ul class="LR_linksList">'.$newline;
        for ($li=0;$li<$relativeCount;$li++){

        /**
        *Ajout pour appel TV
        *
        **/

        $docId = $relArray[$li]['id'];
        $myTv = $modx->getTemplateVar('photo',$fields='*',$docId, 1); //change the tvName to suit your needs
        if($myTv['value'] !=''){
        $relatedOutput .= '<li><a href="[~' . $relArray[$li]['id'] . '~]"><img src="'.$myTv['value'].'" alt="" /></a></li>'.$newline;
        //If you have set an image widget to your TV change this line to
        //$relatedOutput .= '<li><a href="[~' . $relArray[$li]['id'] . '~]">'.$myTv['value'].'</a></li>'.$newline;

        //$relatedOutput .= print_r($myTv);
        }else{
        $relatedOutput .= '<li><a href="[~' . $relArray[$li]['id'] . '~]">' . $relArray[$li]['pagetitle'] . '</a></li>'.$newline;
        }
        //
        }
        $relatedOutput .= '</ul>'.$newline;
        }
        }
        if (empty($relatedOutput)) {
        // there are no related records for this page
        $relatedOutput .= $nonefound.$newline;
        }

        return $relatedOutput;
          Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
          • 27
          • 117 Posts
          Cheers Heliotrope

          Thats absolutely perfect, thank you for your kind help, it is very much appreciated.

          I owe you one!

          0