In AnQiCMS, displaying a list of articles or products under a specific category is a common requirement in website content management.To build subpages under the navigation menu or to display selected content of different modules on the homepage, AnQiCMS provides a flexible and powerful way to achieve this goal.Understanding the content organization logic and the use of template tags will allow you to easily present the required content on the website.
Understanding the content model and categories of AnQiCMS
Firstly, the organization of AnQiCMS content is based on 'content model' and 'category'.Content modelIt is like the 'skeleton' or 'type' of content, for example, the 'article model' is used to publish news, blogs, while the 'product model' is used to display products.Different models can have custom fields to meet the storage needs of different types of content.In the AnQiCMS backend, you can find the management entry for these models and set unique properties for them.CategoryIt is the specific organizational structure under the content model, which is usually hierarchical and can have multiple levels.For example, under the "Article Model", you can create categories such as "Company News", "Industry Dynamics", and so on;Under the "product model", there can be categories such as "electronic products", "home goods", and so on.Each category belongs to a specific content model.
When you need to display specific content on a website, you usually specify the ownership of the contentModel ID(moduleId)andCategory ID (categoryId)to get it accurately. Typically, the corresponding article model ismoduleIdIt could be 1, the product model could be 2, and the specific ID can be viewed on the content model management page in the background.The category ID is generated by the system when creating or editing a category, and you can also find it on the category management page.
Practice operation: Call the list under a specific category in the template
In AnQiCMS template files,archiveListTags are the core tools used to retrieve lists of articles or products. This tag is very flexible and can filter, sort, and display content according to your needs.
To display articles or products under a specific category, you can do this:
{% archiveList archives with moduleId="1" categoryId="5" limit="10" %}
{% for item in archives %}
<li>
<a href="{{item.Link}}">
<img src="{{item.Thumb}}" alt="{{item.Title}}">
<h3>{{item.Title}}</h3>
<p>{{item.Description}}</p>
<span>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
</a>
</li>
{% empty %}
<li>当前分类下暂无内容。</li>
{% endfor %}
{% endarchiveList %}
In this code block:
{% archiveList archives with ... %}It is the start of the list call,archivesIt is a variable name set for the content list obtained.moduleId="1"Specify the content we need to retrieve from the article model (assuming the article model ID is 1). If you need to display products, you can change it to the corresponding product model ID, such asmoduleId="2".categoryId="5"Then it precisely specifies the content we want to obtain under the category with ID 5.You can replace it with the category ID you wish to display. Usually, if you want to include all the subcategory content under the category,childThe parameter is set to default.trueIt does not require additional settings.limit="10"Limited the number of displayed articles or products to 10. You can adjust this number according to the page layout.{% for item in archives %}Loop through each item obtained in the iteration. In the loop,itemrepresenting the article or product currently being processed.{{item.Link}}/{{item.Title}}/{{item.Thumb}}/{{item.Description}}Common fields of content items, representing their links, titles, thumbnails, and summaries, etc.{{stampToDate(item.CreatedTime, "2006-01-02")}}Used to format the publishing time of the content.{% empty %}The part will display a custom prompt when there is no content.
This code can be placed in any template file on your website, such as the homepage, sidebar, or a dedicated list page, as long as it needs to display content for a specific category.
Advanced Technique: Combine Pagination with Dynamic Category Display
Implement pagination display:If there are many items under a category, you may need to paginate the display. Just set thearchiveListlabel'stypethe parameter to"page"and combiningpaginationLabel it:
{% archiveList articles with moduleId="1" categoryId="5" type="page" limit="10" %}
{# 文章列表内容与上述相同 #}
{% for item in articles %}
<li><a href="{{item.Link}}">{{item.Title}}</a></li>
{% endfor %}
{% empty %}
<li>当前分类下暂无内容。</li>
{% endarchiveList %}
<div class="pagination">
{% pagination pages with show="5" %}
<a href="{{pages.FirstPage.Link}}">首页</a>
{% if pages.PrevPage %}<a href="{{pages.PrevPage.Link}}">上一页</a>{% endif %}
{% for page in pages.Pages %}<a class="{% if page.IsCurrent %}active{% endif %}" href="{{page.Link}}">{{page.Name}}</a>{% endfor %}
{% if pages.NextPage %}<a href="{{pages.NextPage.Link}}">下一页</a>{% endif %}
<a href="{{pages.LastPage.Link}}">尾页</a>
{% endpagination %}
</div>
Here, limit="10"It is no longer the total number of displays, but the number of items displayed per page.paginationTags will generate navigation links based on the current page number and total number of pages.
Dynamically obtain the current category ID: If you are on a category page (for examplearticles/list-news.html), and you want to display articles under the current category without hardcodingcategoryId,AnQiCMS will automatically identify the category information of the current page. You can even get more detailed information about the current category throughcategoryDetailtags, such as category title, description, etc.
”`twig {# In the category list page template #} {% categoryDetail currentCategory with name=“Id” %} {# Get the current category ID #}
{% if currentCategory %} {# Make sure to get the category ID #}
{% archiveList articles with moduleId="1" categoryId=currentCategory type="page" limit="10" %}
{% for item in articles %}
<li><a href="{{item.Link}}">{{item.Title}}</a></li>
{% endfor %}
{% empty %}