Textpattern tips, tutorials and code snippets

List articles by most recent comments

Here is a method for displaying a list of articles sorted by the most recent comments. Note: Textpattern 4.07 is required for this tip.

Create an article form

<txp:if_comments>
<txp:if_first_article><ul></txp:if_first_article>
<li><txp:title /> (<txp:comments_count />)</li>
<txp:if_last_article></ul></txp:if_last_article>
</txp:if_comments>

Save the form as article_comments_sort and type article.

The if_comments conditional returns only articles that have comments attached to them. The if_first_article and if_last_article conditionals are used for the wraptag ul.

On your page template

<txp:variable name="articles_with_comments" value='<txp:article form="article_comments_sort" sort="comments_count desc" limit="1" />' />

Place the above variable tag early in your page template, then the following code where you want the article list to display:

<txp:if_variable name="articles_with_comments" value="">
<p>Coming soon: Most active discussions!</p>
<txp:else />
<txp:article form="article_comments_sort" sort="comments_count desc" />
</txp:if_variable>

The if_variable tag is used to check for the existence of the variable name="articles_with_comments" and the value="" attribute is left empty to return the current value.

Thanks to Els and John Stephens for their idea and solution.

Use Textile help to style your comments