0

Modx and blogging part 9: Greatest hits


09
Dec


Modx, Hits, Popular Posts, Articles

How to create a "Popular Posts" list.

The template I've integrated into the site (Caprice) has a "Popular posts" section in the footer that I decided to keep and use. I wanted to base it on the number of hits that an individual blog entry gets and so I went looking for a suitable add-in that could do this for me.

It did not take long to find Hits by JP DeVries. I downloaded it via Package Management and installed it. Be aware that there is a bug in the installation where you don't get a message saying the install completed successfully.

It is quite simple to use. Start by adding the following snippet call:

[[!Hits? &punch=`[[*id]]`]]

to the ArticleTemplate template. By including this in the template that every blog post uses, and by telling it to "punch" based on the id of the post, the number of hits for each blog entry is recorded (in a table in the database).

So having recorded the hit - how to display it? This is a bit more complicated. First of all, we need to call the Hits snippet again:

[[Hits? &parents=`2` &limit=`3` &outSeparator=`,`]]

We're only using three of the many properties here because this is being wrapped into a getResources snippet call in a moment.

&parents=2. This is the id of my Articles container resource.

&limit=3. This tells Hits to only return the top 3 results.

&outSeparator=,. We want a comma separated list returned to getResources.

The following getResources call will return the Resources with the most hits:

[[getResources? 
&parents=`-1` 
&resources=`[[Hits? &parents=`2` &limit=`3` &outputSeparator=`,`]]` 
&tpl=`rg.hitsTpl`]]

&parents=-1. This tells getResources there is no need to look for parents as the Hits snippet call has already done that for us.

&resources= (etc). These are the resources returned by the Hits Snippet as described above. In other words, it will return the most popular resources (blog entries).

&tpl=rg.hitsTpl. This is the chunk to use as the output template. More on that below.

Now that we have the snippet call, we need to place it in the appropriate location. In my case, this is the site footer, for which I have created a chunk - so I only need to add it in once for the whole site.

The template chunk (rg.hitsTpl) will vary depending on your needs. This is mine:

  • [[+pagetitle]]
    [[+publishedon:strtotime:date=`%d %b %Y`]]
  • And this is what it looks like:

    Popular posts chunk

    A couple of points to note:

    1. You could probably extend the Tpl above to include extra information such as the number of hits or the number of comments.
    2. Be aware that Hits will not discriminate between published and unpublished Articles. If you are regularly checking and refreshing new blog posts that are not ready for publication, they may start to appear on the list. If this happens, you can reset the counter as follows:
    [[!Hits? &punch=`22` &knockout=`1`]]
    

    (assuming "22" is the resource id). The hit counter will be set to one, however, as a punch has that default amount.



    Add a Comment