Hi, I've been looking to add a comment count next to article titles in the blog listing, but it doesn't look like that data is exposed by page.find(). The Comments plugin gets that count in the admin panel with the following function:
/**
* Return the latest commented pages
*/
private function fetchPages() {
$files = [];
$files = $this->getFilesOrderedByModifiedDate();
$pages = [];
foreach($files as $file) {
$pages[] = [
'title' => $file->data['title'],
'commentsCount' => count($file->data['comments']),
'lastCommentDate' => date('D, d M Y H:i:s', $file->modifiedDate)
];
}
return $pages;
}
Is there any way I could obtain a similar count for a specific page? I have a workaround that works by using p.title from page.find() to cross-reference pages.title from fetchPages(), but that forces me to loop through every commented page on the site:
{% set articles = page.find('/news').children.order('date', 'desc').slice(0,3) %}
{% for p in articles|slice(0,3) %}
<div class="latest-post">
<h2><a href="{{p.url}}">{{ p.title|e }}</a></h2> — {{ p.date|date("M j, Y") }}
{% for page in grav.twig.pages %}
{% if page.title == p.title %}
– {{page.commentsCount}} comment{% if page.commentsCount > 1 %}s{% endif %}
{% endif %}
{% endfor %}
{{p.content}}
</div>
{% endfor %}
I couldn't find a way to find an array key from a value to avoid looping (something like get_key_for_value(value, array)). What's more, I'm not sure whether Grav allows duplicate names for pages, but I feel like searching based on route would be safer. Any help would be very appreciated.