The output of neighboring documents in MODX Evolution

On many websites after the text of the article, you can often find a block of links to other material on the website. For example, links to "Next article" "Previous article". Also on the product page, in addition to the product, you can see the links to the neighboring items from the same category.

Once I faced a task to take on every product page links to other products in the same category and the next.

Have Ditto as the Wayfinder, such a possibility is not found. So I wrote a simple snippet and decided to share it here. I hope someone it will be useful. The snippet, as was said above, very simple — a great example for those who are just starting to write their solutions to MODX. In code a lot of comments, so that problems with understanding should not arise.


The snippet, you can pass 4 parameters:
the

    &tpl — chunk-template for list of references (required)

    &prevDocs is the number of documents with a lower ID (optional)

    &nextDocs — number of documents with a large ID (optional)

    &sortRevert — the sort order of the documents (optional)



Chunk-pattern:

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



the
 <?php
// define default values 
$prevDocs = (isset($prevDocs)) ? $prevDocs : 2; // the number of neighboring documents with a lower ID
$nextDocs = (isset($nextDocs)) ? $nextDocs : 2; // the number of neighboring documents with a large ID

$sortRevert = (isset($sortRevert)) ? $sortRevert : 0; // the parameter that determines the sorting order

// check  provided  values
$prevDocs = (int) $prevDocs;
$nextDocs = (int) $nextDocs;
$sortRevert = (int) $sortRevert;

// get ID to where the call to this snippet
$id = $modx- > documentIdentifier;

// get the document ID of the parent 
$parent = $modx- > documentObject['parent'];

// check whether the specified chunk
if (!isset($tpl)) {

echo "No chunk defined for siblings-snippet!";
return;

}

// check if the chunk exists
if ($modx- > parseChunk($tpl,array()) === NULL) {

echo "Chunk specified, but not found!";
return;

}

// get data about the neighboring documents with a lower ID
$prev = $modx- > db- > makeArray($modx->db->select('id,pagetitle','modx_site_content',"id < {$id} & parent = {$parent} and published = 1",'id DESC',$prevDocs));

// get data about the neighboring documents with a large ID
$next = $modx- > db- > makeArray($modx->db->select('id,pagetitle','modx_site_content',"id > {$id} & parent = {$parent} and published = 1",'id ASC',$nextDocs));

// combine received this in one array
$siblings = array_merge($next,$prev);

// define array in which to store an indexed array
$indexed = array();

// create array with indexes composed of ID documents
foreach ($siblings as $sibling) {
$indexed[$sibling['id']] = $sibling; 
}

// sortable
if ($sortRevert === 1) {
rsort($indexed);
} else {
sort($indexed);
}

// generated html
foreach($indexed as $sibling){

$html .= $modx- > parseChunk($tpl,array(
'id' => $sibling['id'],
'pagetitle' => $sibling['pagetitle'] 
),
'[+',
'+]'
);

}

echo $html;
?>
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Why I left Google Zurich

2000 3000 icons ready — become a sponsor! (the table of orders)

New web-interface for statistics and listen to the calls for IP PBX Asterisk