Recent articles based off current article category

I added a category field to my frontmatter template to add a category for my blog articles. I now want to display a list of recent articles from the category currently set for the current article (on the article detail page).

New to Ruby and Middleman so not sure how to access this detail. Any help is appreciated.

Thanks!

You can do this using Ruby. I’ll write this out a bit verbosely so it hopefully makes more sense:

# Get the current article's category
category = current_resource.data.category

# Other articles that share the same category
category_articles = blog.articles.select {|a| a.data.category == category && a != current_resource }

# Reverse-sorted by date
sorted_category_articles = category_articles.sort_by {|a| a.date }.reverse

# Top 5
latest_category_articles = sorted_category_articles.first(5)

Now you can loop over latest_category_articles and print them out:

<ul>
<% latest_category_articles.each do |article| %>
   <li><%= link_to article.title, article %></li>
<% end %>
</ul>