How to retrieve and display a list of articles or products in a loop?

Calendar 👁️ 58

Build a vibrant website in AnQiCMS, the display of the content list is an indispensable part.Whether it is to display the latest published articles, hot products, or content under specific categories, AnQiCMS's powerful template engine can help us easily achieve it.This article will delve into how to retrieve and loop through lists of articles or products in AnQiCMS, helping you flexibly build diverse content display pages.

Understanding the content list mechanism of AnQiCMS

AnQiCMS uses a template engine syntax similar to Django, making content display intuitive and easy to master.In AnQiCMS, articles and products are considered a type of content, and they share a flexible content model system.This means that we can use a unified way to retrieve and display lists of different types of content.

The core template tags are the entry points for operating content, which are usually in the form of{% tag %}and the variables to be output are enclosed in{{ variable }}. For displaying content in a loop,forLoop tags are our powerful assistants.

Get the list of articles or products: core tagsarchiveList

To get the list of articles or products,archiveListTags are your preference. This tag is very flexible and can filter, sort, and limit content according to your needs.

UsearchiveListWhen labeling, it is usually specified a variable name to store the list of obtained content, for examplearchivesorproducts. The basic structure is as follows:

{% archiveList archives with moduleId="1" limit="5" %}
    {% for item in archives %}
        {# 在这里展示每个内容项的详细信息 #}
    {% endfor %}
{% endarchiveList %}

Next, we will learn in detail.archiveListSome common parameters, which are the key to custom content lists:

  1. moduleId: Specify the content model.AnQiCMS supports custom content models, common ones include article models (usuallymoduleId="1"), and product models (usuallymoduleId="2"By this parameter, you can specify the type of content you want to retrieve.

    • For example, to retrieve a list of articles:moduleId="1"
    • For example, to retrieve a list of products:moduleId="2"
  2. categoryId: Filter by specific categoryIf you only want to display content under a specific or some specific categories, you can usecategoryId. You can pass the ID of a single category, or multiple IDs separated by commas.

    • For example, get the articles of category ID 10:categoryId="10"
    • For example, get the products of category IDs 10, 12, 15:categoryId="10,12,15"
    • If you want it not to automatically read the current page category ID, but to get the entire site content, it can be set tocategoryId="0".
  3. limit:Control the number of items displayed or the pagination range limitThe parameter is used to limit the number of displayed content. It has two working modes:

    • Limit the number:limit="5"It will only display the first 5 items.
    • Offset mode:limit="2,10"Start from the second item and get the next 10 items (often used in the scenario of 'start displaying from...').
  4. order: Specify the sorting methodThe order of content display is very important.orderParameters allow you to sort based on multiple conditions:

    • id desc: Sort by content ID in descending order (the most recently published content first).
    • views desc: Sort by view count in descending order (popular content first).
    • sort desc: Sort in reverse order according to the custom sorting field set in the background.
  5. typeList type determines the function: typeParameters define the purpose of the list, which affects the way other parameters take effect:

    • type="list"Default mode, only display:limitContent specified in a certain quantity, without pagination.
    • type="page": Combined pagination mode,paginationtags can realize the complete page turning function.
    • type="related": Related content mode, usually used on detail pages, automatically finds other content related to the current content.
  6. flag: Filter recommended attributesWhen publishing content in the background, you can set various recommendation attributes for the content (such as头条[h], 推荐[c], 幻灯[f], etc.). ByflagParameters, you can only display content with specific attributes.

    • For example, only display articles with the "recommended" attribute:flag="c"
    • You can also useexcludeFlagto exclude content with specific attributes.
  7. q: Search keywordsWhentype="page"then,qThe parameter can be used to search for content titles that contain specific keywords. If you include the parameter in the URL?q=关键词AnQiCMS will also automatically read and apply the search.

Loop traversal and data access

Once usedarchiveListGet the content list, and you can go through{% for item in archives %}such a structure to access each item in the list one by one. Inside the loop,itemThis is the current content object, you can access various properties such as:

  • {{ item.Id }}: Content ID
  • {{ item.Title }}: Content Title
  • {{ item.Link }}: Content Link
  • {{ item.Description }}: Content Summary/Description
  • {{ item.Thumb }}Content thumbnail
  • {{ item.Logo }}Content cover large image
  • {{ item.Views }}Content views
  • {{ stampToDate(item.CreatedTime, "2006-01-02") }}Formatted publish time(stampToDateis a date formatting label,"2006-01-02"Is the time formatting standard of Go language

Example: Display the latest article title and link

<div class="latest-articles">
    <h3>最新文章</h3>
    <ul>
        {% archiveList articles with moduleId="1" order="id desc" limit="5" %}
            {% for item in articles %}
                <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
            {% empty %}
                <li>暂无文章</li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</div>

Implement pagination function:paginationTag

For lists with a large amount of content, pagination is the key to improving user experience. WhenarchiveListlabel'stypethe parameter to"page"It will cooperate when,paginationLabels work together to implement a complete pagination navigation.

paginationHow to use labels:

{% pagination pages with show="5" %}
    {# 在这里组织分页链接,例如上一页、下一页、页码等 #}
{% endpagination %}

paginationThe label will create onepagesAn object that contains rich pagination information:

  • pages.TotalItemsTotal number of contents:
  • pages.TotalPagesTotal number of pages:
  • `pages.Current

Related articles

How to dynamically generate and display the top or bottom navigation menu of a website?

In website operation, the top and bottom navigation menus play a vital role. They are not only guides for users to explore the website's content but also crucial for search engines to understand the website's structure.A well-designed, dynamically generated navigation menu that can greatly enhance user experience and website maintainability.AnQiCMS provides a set of intuitive and powerful features to help you easily achieve this goal.### Backend Configuration: Bring Your Menu to Life To dynamically generate and display navigation menus in AnQi CMS, you first need to configure the menu content in the backend

2025-11-07

How to generate and display multi-level breadcrumb navigation in AnQi CMS?

In website content operation, a clear navigation path is crucial for user experience and search engine optimization.A good breadcrumb navigation can help users quickly understand the current page's position in the website structure and make it convenient for them to backtrack to the previous level or higher-level pages.AnQiCMS (AnQiCMS) fully understands this, therefore it provides a powerful and easy-to-use breadcrumb navigation feature that allows website administrators to easily generate and display multi-level breadcrumbs.The core of implementing breadcrumb navigation in Anqi CMS lies in its built-in `breadcrumb` tag

2025-11-07

How to configure and display SEO title, keywords, and description (TDK) on the website homepage?

In website operation, the search engine optimization (SEO) of the homepage is crucial.A well-configured SEO title, keywords, and description (TDK) can significantly improve the visibility of a website in search engine results pages (SERP), attracting more potential visitors.AnQiCMS (AnQiCMS) is a system designed for SEO-friendliness, providing an intuitive and convenient way to manage key information.This article will guide you to understand how to configure and display SEO title, keywords, and description on the homepage of your AnQiCMS website.--- ### One

2025-11-07

How to retrieve and display the contact information set in the background (such as phone number, email, WeChat)?

In website operation, clearly displaying contact information is crucial for enhancing user trust and promoting interaction.Whether it is to consult products, seek support, or engage in business cooperation, convenient contact channels can greatly improve conversion rates.AnQiCMS (AnQiCMS) understands this and provides flexible and powerful features, allowing you to easily obtain and display various contact information in website templates.Next, let's take a look at how to display the contact information, such as phone, email, WeChat, etc., set in the background of Anqi CMS, naturally and smoothly on your website

2025-11-07

How to display a list of articles or products under a specific category in AnQiCMS?

In AnQiCMS, displaying a list of articles or products under a specific category is a common need in website content management.To build subpages under the navigation menu or to showcase selected content from different modules on the homepage, AnQiCMS offers 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 AnQiCMS content model and categories Firstly, the organization of AnQiCMS content is based on "content model" and "categories"

2025-11-07

How to display document recommendation attributes (such as headline, recommendation) on the document list or detail page?

It is crucial to highlight the key content on the website in a content management system to attract users and increase page views.AnQiCMS (AnQiCMS) provides powerful document recommendation attribute functions, allowing website operators to flexibly mark and display articles.This article will introduce how to cleverly use these recommended attributes in the document list and detail page of Anqi CMS to make your website content more vivid and guiding.### Learn about the recommended features of AnQi CMS AnQi CMS has designed various recommended features for each document

2025-11-07

How to display the detailed content of articles or products and support lazy loading of images?

A Guide to Displaying Content Details and Lazy Loading Images in AnQi CMS in Today's Digital World, the way websites present content directly affects user experience and search engine rankings.Whether it is a detailed article introduction or a beautiful product display, how to present the content efficiently and aesthetically to the visitors and ensure the website loading speed is a challenge that every operator needs to face.AnQiCMS (AnQiCMS) is a powerful content management system that provides us with flexible tools to meet these challenges.This article will delve into how to fully utilize the functions of Anqi CMS

2025-11-07

How to get and display the previous/next content of the current document?

In content-based websites, providing visitors with a convenient navigation experience is crucial, among which the 'Previous' and 'Next' functions are standard features of the article detail page.They can not only guide users to continue browsing more content, effectively increase the website's PV (Page View), but also help search engines better understand the structure of the website's content to optimize SEO performance.AnQiCMS fully considered this requirement, providing us with very simple and intuitive template tags to easily implement this feature.### Learn about AnQiCMS template mechanism At

2025-11-07