Manually Ordered Blog Articles

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:

[[!ArticlesMenuOrder? &docs=`7,18,15,17,10,16`]]

Add these Wayfinder properties to the Wayfinder tag:

&sortBy=`menuindex`
&sortOrder=`ASC`

.

If you’re using getResources, use this property in the getResources tag:

&sortby=`{"menuindex":"ASC"}`

Then, create a snippet called ArticlesMenuOrder with this code:

<?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++;
    }
}

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:

[[!ArticlesMenuOrder? &docs=`7,18,15,17,10,16`]]
<h3>Finished setting menuindex fields</h3>

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!

Tags: , , , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray

I am the author of MODX: The Official Guide and over 30 MODX add-on components. I host Bob's Guides, a source of valuable information for MODX users, and I've been very active in the MODX Forums with over 14,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *


× 5 = 5

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>