How to loop and display the latest 10 articles in the template?

Calendar 👁️ 54

As an experienced CMS website operation personnel, I fully understand the core position of content in website operation.Dynamic display of the latest content not only improves user experience, but also effectively increases the activity of the website and the frequency of search engine crawling.Below, I will elaborate on how to loop and display the list of the latest 10 published articles in the AnQiCMS template.

Display the latest 10 articles in the AnQiCMS template loop

In AnQiCMS, the creation and management of templates follow a set of simple and efficient rules, with syntax similar to the Django template engine, making it easy for front-end developers to get started.To dynamically display the latest 10 articles on the website page, we need to use the built-in template tags and loop structure of AnQiCMS.

The template files of AnQiCMS are usually named with.htmlsuffix and stored in/templateIn the specific template folder under the directory. Static resources such as styles, scripts, and images are placed in/public/static/directory. Variables are used with double curly braces in template files.{{变量}}Definition, while logical tags such as conditional judgment and loop control use single curly braces and the percent sign{% 标签 %}Definition, and it requires using end tags to close, for example{% for ... %}required{% endfor %}.

To obtain the article list, we mainly rely on the AnQiCMS providedarchiveListThe tag is a powerful tool for obtaining lists of articles, products, and other content.By reasonably configuring its parameters, we can accurately control the quantity, sorting method, and category of the required content.

Firstly, we need to use in the template file where we want to display the latest articles,archiveListTags to retrieve data. This tag allows us to specify multiple conditions to filter articles, the most critical of which is limiting the number and sorting method.In order to get the latest 10 published articles, we willtypethe parameter to"list"(indicating to get a list instead of pagination),limitthe parameter to"10"(limiting to get 10 articles), and useorderthe parameter specifies the sorting method as"CreatedTime desc"This means that the articles will be sorted in descending order by creation time, ensuring that the most recently published articles are at the top. In addition,moduleIdThe parameter is used to specify the content model, articles usually use the default article model ID, generally no special specification is required or filled according to actual configuration.

{# 获取最新发布的10篇文章 #}
{% archiveList archives with type="list" limit="10" order="CreatedTime desc" %}
    {# 检查是否有文章存在,如果列表为空,则显示“没有内容” #}
    {% for item in archives %}
        <div class="latest-article-item">
            {# 文章标题及链接 #}
            <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
            {# 文章发布时间,使用stampToDate标签格式化 #}
            <p class="article-date">发布于:{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}</p>
            {# 文章简介 #}
            <p class="article-description">{{ item.Description }}</p>
            {# 如果文章有缩略图,则显示 #}
            {% if item.Thumb %}
                <div class="article-thumbnail">
                    <img src="{{ item.Thumb }}" alt="{{ item.Title }}">
                </div>
            {% endif %}
            {# 可以根据需要添加更多文章信息,例如分类、浏览量等 #}
            <p class="article-meta">
                {% if item.CategoryId %}
                    {# 获取文章所属分类的名称和链接 #}
                    <span>分类:<a href="{% categoryDetail with name='Link' id=item.CategoryId %}">{% categoryDetail with name='Title' id=item.CategoryId %}</a></span>
                {% endif %}
                <span>浏览量:{{ item.Views }}</span>
            </p>
        </div>
    {% empty %}
        <p>目前还没有任何最新文章发布。</p>
    {% endfor %}
{% endarchiveList %}

In the above code,{% archiveList archives with type="list" limit="10" order="CreatedTime desc" %}This line of code is the core, it retrieves the article data that meets the conditions from the database and stores the result in a variable namedarchives. Then,{% for item in archives %}Loop through thisarchivesThe array, each loop will assign the data of the current article toitemVariable.

We can access inside the loopitemThe various properties of the object to display the detailed information of the article, such as{{ item.Title }}Get the article title,{{ item.Link }}Get the link to the article detail page,{{ item.Description }}Get the article summary. Specifically,{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}This line of code shows how to usestampToDatetags to format Unix timestamps into readable dates and times. Among them,"2006-01-02 15:04"Is a date formatting string unique to Go language. We also pass{% if item.Thumb %}Determine if the article has a thumbnail and display it. By nestingcategoryDetailTags, can obtain the name and link of the category of each article.

IfarchiveListThe list of articles obtained is empty,{% empty %}The content within the tag will be executed, providing friendly prompt information. Finally,{% endarchiveList %}and{% endfor %}Tags are used to closearchiveListandforLoop.

In practice, be sure to place the above code snippet in your AnQiCMS template file (for exampleindex.htmlor you define a certain public snippet file). At the same time, please ensure that your template file uses UTF-8 encoding to avoid garbled characters issues.Any modification to the template file may require you to click the "Update Cache" button in the background management interface to ensure that the content of the front-end page is refreshed in a timely manner.In this way, your website can dynamically display the latest and most attractive content, thus better serving your readers.


Frequently Asked Questions (FAQ)

How to modify the number of displayed articles or adjust the sorting method?

You can adjust byarchiveListin the labellimitandorderParameters can easily be implemented. For example, if you want to display 5 articles, you can setlimit="10"changed tolimit="5". If you wish to sort the articles by the number of views in descending order (the most popular articles), you canorder="CreatedTime desc"changed toorder="views desc". These parameters provide great flexibility to meet your different content display needs.

How do I display the latest articles under a specific category?

InarchiveListthe tag withcategoryIdJust use the parameter. For example, if you only want to display the latest articles of category ID 10, you can setarchiveListthe tag to{% archiveList archives with type="list" limit="10" order="CreatedTime desc" categoryId="10" %}. You can also separate multiple category IDs with a comma, such ascategoryId="10,12,15", to display the latest articles under multiple specified categories.

3. Can this code be used to display other content models besides articles, such as the latest products?

Can be. The design of AnQiCMS allows the reuse of the same list tags for different content models. If you want to display the latest products, you need to know the corresponding product model formoduleId. Usually, the article model'smoduleIdis 1, the product modelmoduleIdis 2 (specific values please refer to your AnQiCMS backend content model configuration). Just set thearchiveListput in the tag.moduleIdparameter to the product model's ID, for example{% archiveList products with type="list" limit="10" order="CreatedTime desc" moduleId="2" %}, and thenarchivesVariable name changedproducts(or any name you like) to keep the code clear, you can display the latest product list.

Related articles

How to get the title, content, and link of the current page document?

As an experienced website operator who deeply understands the operation of AnQiCMS, I know that the accuracy and efficiency of obtaining the current page content are crucial for the daily management and optimization of the website.AnQiCMS provides intuitive and powerful template tags, allowing us to easily extract the required document title, specific content, and standard link of the current loaded page.This not only helps us to have fine control over the front-end display, but also provides a solid foundation for SEO optimization.### Get the document title of the current page The document title of the current page usually has two levels of meaning

2025-11-06

What types of custom fields does Anqi CMS support? For example, 'Single line'

As an experienced website operator for AnQiCMS, I am well aware of the importance of the flexibility of the content management system, especially the custom field function, which can greatly meet the personalized needs of content structure and display in different business scenarios.AnQiCMS provides quite comprehensive support in this aspect, allowing us to build rich content types according to our actual needs.One of the core strengths of AnQiCMS is its flexible content model.This means that the system not only provides preset content types such as articles and products, but also gives us the power to create or modify content models according to our business characteristics

2025-11-06

What specific prompts does the 'Title Name' field of the custom content model have when publishing documents?

As an experienced website operator proficient in Anq CMS, I know that every setting of the background field may have a cascading effect on the efficiency of content operation and the ultimate user experience.In the custom content model, the 'Title Name' field, although seemingly trivial, plays a crucial guiding role in practice, greatly affecting the accuracy of content creation and the smoothness of the process.AnQi CMS, with its high flexibility and customizability, provides us with the ability to build a diverse content structure based on different business needs.No matter if it's articles, products, activities, or any other customized information type

2025-11-06

How to set the 'URL alias' of the content model to better support search engine optimization (SEO)?

As an experienced AnQiCMS (AnQiCMS) website operations personnel, I know that every detail can affect the performance of the website in search engines.The URL structure, especially the alias setting of the content model's URL, is a crucial element that should not be overlooked.A clear, meaningful, and search engine-friendly URL not only enhances user experience but is also the foundation of website SEO optimization.### How to optimize website visibility in search engines: How to set the URL alias of the content model In today's increasingly fierce digital marketing

2025-11-06

How to filter and display the article list according to a specified category ID?

As an experienced CMS website operation person, I know the importance of content organization and display for user experience and SEO.In daily work, we often need to filter and display article lists according to specific categories so that readers can quickly find the content they are interested in.Today, I will elaborate on how to flexibly and efficiently filter and display article lists in AnQiCMS according to specified category IDs.The categorization of content is the foundation for the effective operation of a website.

2025-11-06

How to implement pagination for the article list display function?

As a website operator who is deeply familiar with the operation of AnQiCMS, I understand that it is crucial to organize and present a large number of articles efficiently in content display.The pagination feature not only greatly enhances the user's browsing experience, but also avoids the visual fatigue caused by too much content on a single page, and is an indispensable part of the SEO strategy.To implement pagination display of article lists in AnQiCMS, it benefits from its flexible template engine and dedicated tag system, making the whole process clear and efficient.

2025-11-06

How to obtain the global system configuration information such as website name, Logo, etc?

As a website operator proficient in AnQi CMS content management, publishing, and optimization, I fully understand the importance of obtaining global website configuration information for website construction and daily maintenance.This information constitutes the basic framework of the website, including brand identification, contact information, SEO metadata, etc., ensuring the uniformity, professionalism, and maintainability of the website.In Anqi CMS, obtaining these global system configuration information is intuitive and flexible, mainly achieved through built-in template tags.

2025-11-06

How to display first and second level menus and their links in the website's main navigation?

As an experienced website operator, I am well aware of the importance of a clear and intuitive navigation system for website user experience and content discovery.AnQiCMS (AnQiCMS) with its flexible content management features, allows us to easily build navigation structures that meet various business needs.Today, I will explain in detail how to cleverly set up and display the first and second-level menus and their links in the main navigation of the website based on the powerful functions of AnQiCMS.

2025-11-06