We received a lot of questions how to show most popular articles and posts regarding our tutorial about implementing page view counter in SilverStripe.
Here is the solution that we also implemented on our blog system.
Let's say that we have blog article page represented with class BlogPostPage and implemented logic for page hit/view counter.
- Advertisement -
How to display most popular pages
1) Create new method in the page class
/**
* Method returns most popular blog articles
*
*/
public function MostPopularBlogArticles($num = 5) {
return DataObject::get(
"BlogPostPage",
"Status = 'Published'",
"`PageCounter`.Counter DESC",
"INNER JOIN PageCounter ON `PageCounter`.PageID = `BlogPostPage`.ID",
$num
);
}
2) Add this code to the blog post page template file (BlogPostPage.ss in our case)
<!-- Most Popular Blog Articles -->
<div>
<h3>Popular Articles</h3>
<ul>
<% control MostPopularBlogArticles(5) %>
<li>
<a href="$Link" title="$Title.XML">$MenuTitle.XML</a>
</li>
<% end_control %>
</ul>
</div>
Note: Do not forget to change class name in the code above if you want to implement most popular pages logic on your SilverStripe powered website.
- Advertisement -