- 1. Collection Queries
- 2. Taxonomy Queries
- 3. Asset Queries
- 4. User Queries
- 5. Globals Queries
- 6. Relationship Queries
- 7. Search Queries
Statamic, a flexible CMS built on Laravel, offers a rich set of content querying capabilities. These queries are essential for fetching and displaying content based on various criteria and conditions, making your website dynamic and interactive. Understanding the different types of content queries in Statamic can help you leverage its full potential. Let's dive into some of the key content queries available in Statamic.
1. Collection Queries
Collections in Statamic are groups of related content, similar to posts in a blog. Collection queries are used to retrieve entries from these collections.
Example:
{{ collection:blog as="posts" }}
{{ posts }}
<article>
<h2>{{ title }}</h2>
{{ excerpt }}
</article>
{{ /posts }}
{{ /collection:blog }}
This query fetches entries from the 'blog' collection and displays their title and excerpt.
2. Taxonomy Queries
Taxonomies in Statamic allow you to group content similarly to categories and tags in other CMSs. Taxonomy queries help you retrieve content associated with specific taxonomy terms.
Example:
{{ taxonomy:categories slug="news" }}
{{ title }}
{{ content }}
{{ /taxonomy:categories }}
This fetches entries categorized under the 'news' category.
3. Asset Queries
Assets in Statamic refer to media files like images and documents. Asset queries let you retrieve and display these files.
Example:
{{ asset_container:main_assets }}
{{ assets }}
<img src="{{ url }}" alt="{{ alt }}">
{{ /assets }}
{{ /asset_container:main_assets }}
This query displays images from the 'main_assets' asset container.
4. User Queries
User queries are used to fetch data about users registered on your Statamic site.
Example:
{{ users:all }}
{{ name }} - {{ email }}
{{ /users:all }}
This lists all registered users along with their names and email addresses.
5. Globals Queries
Globals in Statamic are pieces of content that are global to the site, like site name, logo, or footer content. Global queries fetch this type of content.
Example:
{{ global:set name="site_settings" }}
<footer>
<p>{{ site_settings:footer_text }}</p>
</footer>
{{ /global:set }}
This fetches and displays the footer text defined in the 'site_settings' global.
6. Relationship Queries
Statamic allows you to establish relationships between different content types. Relationship queries let you fetch and display related content.
Example:
{{ collection:blog as="posts" }}
{{ posts }}
<article>
<h2>{{ title }}</h2>
{{ related_articles }}
<a href="{{ url }}">{{ title }}</a>
{{ /related_articles }}
</article>
{{ /posts }}
{{ /collection:blog }}
This query displays related articles for each blog post.
7. Search Queries
Search queries in Statamic enable you to implement site-wide search functionality.
Example:
{{ search:results }}
<article>
<h2>{{ title }}</h2>
{{ excerpt }}
</article>
{{ /search:results }}
This performs a search and displays the results.
Statamic's diverse querying capabilities provide immense flexibility in how you retrieve and display content. Whether managing a blog, a corporate website, or an e-commerce platform, understanding these content queries can significantly enhance your site's functionality and user experience.