MODX Upcoming Article Teaser

A MODX Forum user asked for a way to show upcoming bMODX logolog articles as a teaser to get people to return to the site. In this article we’ll look at how to use the pub_date field to display an arbitrary number of upcoming articles in the order that they’ll be published. If there are no upcoming articles, we will display nothing at all.

Note that it’s possible to do this with getResources, but it’s tricky and somewhat slower. When using getResources, it’s also more difficult to show nothing when there are no upcoming articles without resorting to a conditional output modifier, which will slow things down even more.

The Tag

Here’s the tag you’ll put in your blog container Tpl and/or your blog post Tpl chunk:

[code language=”html”]


The &parent property should be set to the ID of the blog container resource. The &limit property determines how many upcoming articles will be displayed. If it’s set to 0, it will show all of them.


Tpl Chunks

We’ll use an outer and an inner Tpl chunk. The outer Tpl will be a wrapper for the list of upcoming articles. The inner Tpl will provide a format for each individual article listing.

Here’s the outer Tpl chunk:

[code language=”html”]
<div class="upcoming_teaser_div">
<h3>Upcoming Articles</h3>

<div id="upcoming_teaser_outer">
<ul id="upcoming_teaser_ul">


Here’s the inner Tpl chunk:

[code language=”html”]
<li class="upcoming_teaser_li">
[[+pagetitle]] ([[!+pub_date:strtotime:date=`%a %b %d, %Y`]]) &mdash; [[+introtext]]

These are minimal chunks. Feel free to modify them to meet your needs. The snippet will set placeholders for all fields. If you need to include any TVs, you’ll have to modify the snippet to set placeholders for them.

Notice that we’ve put the div and h3 tags in the Tpl chunk rather than the page template. This is necessary so that we can show nothing if there are no articles. For some page designs, you may have to move those to the page template in order to make the layout work the way you want it to.



The snippet simply gets the requested number of articles and returns the formatted list of them (or nothing if no articles are found).

[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,

/* Create the query */
$query = $modx->newQuery(‘Article’);

/* 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(‘Article’, $query);

if (empty($docs)) {
/* No upcoming articles, return nothing */
$output = ”;
} else {
/* No use getting this unless we have articles */
$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_teaser_inner]]’, $inner, $outer);

return $output;


How It Works

Most of the code is explained in the comments above. The code sets up the query, gets the articles, and iterates through them. As it grabs each article, it creates an array of the articles fields with toArray(), then retrieves the inner Tpl chunk with the fields as a second argument so the placeholders in the chunk will be replaced by the field values for that resource. Each formatted entry is then added to the $inner string variable. Finally, str_replace() replaces the single placeholder in the outer Tpl chunk with the content of the $inner variable and we set the $output variable to the result, then return it for display.

It’s worth noting that in the where clause of the query, the 'published' => '0' line is not really necessary. MODX zeros out the pub_date field when it marks the article as published, so the test of pub_date should make sure no published articles appear in the list. Adding that line is not a bad idea, though. It cuts down the number of records MySQL has to perform the date comparison on, potentially speeding up the query. It also protects against edge cases where the article somehow gets published early (either in code, or directly in the database), without the pub_date field being reset.


Coming Up

The code of this snippet is tied to blog articles that have Article in their class_key field. In the next blog post, we’ll look at how to modify it to work with other resource classes.


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 *