📜 ⬆️ ⬇️

Displaying neighboring documents in MODX Evolution

On many sites after the text of the article you can often find a block of links to other materials on the site. For example, the links “Next article”, “Previous article”. Also on the product page, in addition to the product itself, you can see links to neighboring products from the same category.

Once I faced the task to display on each product page links to other products in the same category, and neighboring ones.

In Ditto, as in Wayfinder, I have not found such an opportunity. Therefore, I wrote a simple snippet and decided to share it here. I hope someone will find it useful. Snippet, as mentioned above, is very simple - a great example for those who are just starting to write their solutions for MODX. There are a lot of comments in the code, so there should be no problems with understanding.

')
Snippet can transfer 4 parameters:


Chunk pattern:

<a href="[~[+id+]~]">[+pagetitle+]</a> 



  <?php //     $prevDocs = (isset($prevDocs)) ? $prevDocs : 2; //      ID $nextDocs = (isset($nextDocs)) ? $nextDocs : 2; //      ID $sortRevert = (isset($sortRevert)) ? $sortRevert : 0; //     //       $prevDocs = (int) $prevDocs; $nextDocs = (int) $nextDocs; $sortRevert = (int) $sortRevert; //  ID        $id = $modx->documentIdentifier; //  ID - $parent = $modx->documentObject['parent']; //     if (!isset($tpl)) { echo "No chunk defined for siblings-snippet!"; return; } //     if ($modx->parseChunk($tpl,array()) === NULL) { echo "Chunk specified, but not found!"; return; } //        ID $prev = $modx->db->makeArray($modx->db->select('id,pagetitle','modx_site_content',"id < {$id} and parent = {$parent} and published = 1",'id DESC',$prevDocs)); //        ID $next = $modx->db->makeArray($modx->db->select('id,pagetitle','modx_site_content',"id > {$id} and parent = {$parent} and published = 1",'id ASC',$nextDocs)); //       $siblings = array_merge($next,$prev); //         $indexed = array(); //       ID  foreach ($siblings as $sibling) { $indexed[$sibling['id']] = $sibling; } //  if ($sortRevert === 1) { rsort($indexed); } else { sort($indexed); } //  html foreach($indexed as $sibling){ $html .= $modx->parseChunk($tpl,array( 'id' => $sibling['id'], 'pagetitle' => $sibling['pagetitle'] ), '[+', '+]' ); } echo $html; ?> 

Source: https://habr.com/ru/post/141261/


All Articles