MODX Upcoming Teaser Outside of Articles

In the previous article, we looked at a way to display a “teaser” for MODX logoupcoming blog posts, but what if you want to use the same idea for non-articles? In this one, we’ll modify the code slightly to allow it to work with other resource types. This will also be a good example of how to modify a snippet while maintaining backward compatibility.

Tags and Chunks

In order to maintain backward compatibility, neither the tag nor the Tpl chunks needs to be modified, though you’ll have to add a classKey property and modify the &parent property if you want to use the snippet with a non-article resource type.

[code language=”html”]



New Code

In order to use the snippet with other resource types, all we need to do is add a &classKey property to the snippet tag, and use its value instead of ‘Article’ in the snippet. To maintain backward compatibility We’ll set ‘Article’ as the default value for the property so the snippet will work with no changes for blog articles.

[code language=”php”]
/* UpcomingTeaser snippet */

/* Save some typing and space */
$sp = $scriptProperties;

/* Get basic info from the properties */
$limit = $modx->getOption(‘limit’, $sp, ‘0’);
$parent = $modx->getOption(‘parent’, $sp, null);
$outerTpl = $modx->getOption(‘outerTpl’, $sp,
$innerTpl = $modx->getOption(‘innerTpl’, $sp,

$classKey= $modx->getOption(‘classKey’, $sp, ‘Article’, true);

/* Create the query */
$query = $modx->newQuery($classKey);

/* We only want articles that are unpublished children of
the blog container, and have a pub_date later than the
current time */
‘parent’ => $parent,
‘published’ => ‘0’,
‘pub_date:>=’ => time(),


/* This will make sure they are in chronological order */
$query->sortby(‘pub_date’, ‘ASC’);

/* Set the number of articles to be retrieved */

/* Get the articles */
$docs = $modx->getCollection($classKey, $query);

if (empty($docs)) {
/* No upcoming resources, return nothing */
$output = ”;
} else {
/* No use getting this unless we have something to show */
$outer = $modx->getChunk($outerTpl);

/* Iterate through the docs formatting as we go */
foreach($docs as $doc) {
$fields = $doc->toArray();
$inner .= $modx->getChunk($innerTpl, $fields);
/* Plug the articles list into the outer Tpl chunk */
$output = str_replace(‘[[+upcoming_inner]]’, $inner, $outer);

return $output;



The only changes are the line that gets the property value, the line where we create the query, and the line where we call getCollection() to get the resources.


classKey Settings

We’ve made the &classKey a property so the code could be used for other document classes. You might have a custom resource class, or you might just want to use the code to create a teaser for regular web pages rather than blog articles. In that case, you’d just set the &classKey property to the class you want to use.

Note that if you use &classKey=`modResource`, you’ll get all upcoming resources, including weblinks, symlinks, static resources, blog articles, and any other objects that extend modResource. If you want just documents and not blog articles, use &classKey=`modDocument`.

Coming Up

What if you want to display both blog posts and other documents (say, on the site’s home page)? In the next article we’ll modify the snippet to allow multiple class keys.


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 <a
href=””>Arvixe Web Hosting!

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

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

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