I just did a little test with pages and posts, which worked just fine. I assume you could also combine categories as well. Make sure you think through the logic very carefully with this approach. Pages, posts, and categories may have their own unique YAML front matter, so be sure to think about the display logic.
For example, If all your posts contain front matter with, say, featuredImage
, but pages and categories do not have that, then you may have to write some custom logic before displaying content from each type. Hopefully, that makes sense
To get all the pages, use this code:
{% assign pages = site.pages %}
However, I only like to get pages that have a title associated with them, so here’s a light modification if that is important to you:
{% assign pages = site.pages | where_exp: "item", "item.title" %}
Next, you will want all your posts, so add the following line:
{% assign posts = site.posts %}
Note that you rarely have to check for a title
in posts because it is a required field, and if you do not add it to the front matter, the title is derived from the filename.
Finally, combine the arrays using the Liquid concat
filter with the following code:
{% assign pagesAndPosts = pages | concat: posts %}
Here is a little code snippet that will traverse the new pagesAndPosts
array and display the title for each item:
{% for pageAndPost in pagesAndPosts %}
title: {{pageAndPost.title}}
{% endfor %}
As I mentioned earlier in this response, I was not able to test with categories. Still, I assume if you want to build a pagesPostsAndCategories
array, it would look like the following code:
{% assign pagesPostsAndCategories = pages | concat: posts | concat: categories %}