Newscoop 3 Cookbook

List of popular articles (most read)

For many publications, a listing of the most popular articles is an easy way to keep readers on the site, thus increasing both page views and time on site. Newscoop has an internal statistics mechanism which counts page views (the mechanism counts each URL request as a "read"), and that mechanism can be called in other templates.

In this case, we will call the available functions to make a list of popular articles, which can then be arranged on a page like the screenshot below.

The following code snippet is taken from The Journal's front page, from the sub-template set_thejournal/_tpl/front_tabs.tpl

It is displayed inside a jQuery tab which displays comments together with the most read articles, which explains why the CSS <div> tags are the same for both. Read the chapter List of latest comments for instructions on how to make that template, and read the chapter on Tabs with jQuery for instructions on how to make the tab.

This will do the following:

  • Assign the styling for the tab box
  • Set the issue to the current one
  • Return a list of five articles, ordered by popularity in descending order
  • Set a constraint for the article type of 'news'
  • Get the URI for the article
  • Display the article name
<div id="tabs" class="block">
  <ul class="idTabs wrap tabs">
    <li><a class="selected" href="#commented">Most Read</a></li>
    <li><a href="#recentcomments">Recent Comments</a></li>
  </ul>
  <div class="inside">
    <ul style="display: block;" id="commented">
{{ local }}
{{ set_current_issue }}
{{ list_articles length="5" order="bypopularity desc" constraints="type is news" }}
    <li><a href="{{ uri options="article" }}">{{ $gimme->article->name }}</a></li>
{{ /list_articles }}
{{ /local }}

Here is another example, which will tell Newscoop to do the following:

  • List the ten most popular articles
  • Those articles must have the article type of 'article'
  • They must be from sections numbered between 20-300
  • They must only be from the last five issues
  • They will be ordered in descending order according to their popularity
{{ list_articles length="10" constraints="type is article section greater 19 section smaller 301 issue greater `$gimme->issue->number-5`" order="bypopularity desc" }}

Note the use of the backtick character in the code above. The next snippet will list the ten most popular articles in the last seven days. It will:

  • Assign a variable for the date that is seven days in the past
  • List the ten most popular articles in descending order
  • Set constraints so that the dates of the articles returned are within the time we set in the variable
  • Return articles regardless of issue and section 
{{assign var="xdate" value="-7 days"|date_format:"%Y-%m-%d"}}
{{list_articles length="10" order="bypopularity desc" constraints="publish_date greater $xdate reads greater 0" ignore_issue="true" ignore_section="true"}}

The following example will:

  • Assign a variable for a date one month in the past
  • List the ten most popular articles in descending order
  • Set constraints where the article type is 'article,' and the publish date is within the time we set in the variable
  • Return articles regardless of issue
  • Return articles regardless of section
{{assign var="xdate" value="-1 month"|date_format:"%Y-%m-%d"}}
{{list_articles length="10" order="bypopularity desc" constraints="type is article publish_date greater $xdate reads greater 0" ignore_issue="true" ignore_section="true"}}