Suppose you want to display the documents in your Articles blog in a certain order on the blog’s home page or in a menu. Maybe you also want to periodically change that order manually.
If you want to order regular Resources displayed with a Wayfinder menu or a getResources tag, you can drag them in the tree to the order you want and tell the snippet to order them by menuindex
.
With Articles, though, you typically can’t see the articles in the tree and you can’t see the menuindex
field when editing the articles (at least not in the current version). You could set the menuindex
field in the front end with NewsPublisher or set it manually in PhpMyAdmin, but those aren’t very convenient alternatives.
Here’s another way (actually two ways). The first method below will set the menuindex
field for a set of Resources on the fly as the page is loaded, but before the Resources are retrieved by Wayfinder or getResources. The second (much better) method will set them once and assume that they will stay set (which they almost always will).
On-the-fly Method
Put this snippet tag above any Wayfinder or getResources tag that displays articles in a template or Tpl chunk that:
[code language=”html”][[!ArticlesMenuOrder? &docs=`7,18,15,17,10,16`]][/code]
Add these Wayfinder properties to the Wayfinder tag:
[code language=”html”]
&sortBy=`menuindex`
&sortOrder=`ASC`
[/code]
.
If you’re using getResources, use this property in the getResources tag:
[code language=”html”]&sortby=`{"menuindex":"ASC"}`[/code]
Then, create a snippet called ArticlesMenuOrder with this code:
[code language=”php”]<?php
/* ArticlesMenuOrder snippet */
$docs = explode(‘,’, $scriptProperties[‘docs’]);
$i = 0;
foreach ($docs as $doc) {
$modx->getObject(‘Article’, $doc);
if ($doc) {
$doc->set(‘menuindex’, $i);
$doc->save();
$i++;
}
}
[/code]
This is a relatively slow solution because the snippet will execute on every page load, but it’s pretty much guaranteed to work. It’s only really necessary if your Articles show in the resource tree, and even then the method below should still work.
A Faster, Better Method
Here is a much faster and even more convenient alternative that should work in almost all situations. In this method, you create a new resource containing just the ArticlesMenuOrder snippet tag. Viewing that resource in the front end will set the menuindex
fields for you.
If you implemented the first method above, remove the ArticlesMenuOrder tag from any templates or Tpl chunks (but leave the properties in the Wayfinder and/or getResources tags). If you didn’t implement the first method, add the properties described above to any Wayfinder or getResources tags that show Articles.
Create a new resource (not an article) called SetArticlesMenuindex
with just this code:
[code language=”html”]
[[!ArticlesMenuOrder? &docs=`7,18,15,17,10,16`]]
<h3>Finished setting menuindex fields</h3>
[/code]
When you want to set or change the order for your Articles, just edit the SetArticlesMenuindex
resource, list the article IDs in the desired order in the tag, save the resource, and then click on the “View” button at the upper right. That will set the menuindex
fields for those Articles and they should stay set until you change them. MODX generally won’t change them as long as the Articles don’t show in the Resource tree. You can always restore their order by viewing the
SetArticlesMenuindex
resource.
For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.
Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!