How to judge and highlight the first article in the AnQiCMS article list loop?

Calendar 👁️ 52

In website content display, we often encounter the need to handle the first element of the article list specially.In order to highlight visually, achieve unique layout design, or give the first article higher attention, AnQiCMS provides flexible and powerful template tags to help us easily achieve this goal.

AnQiCMS uses a syntax similar to the Django template engine, when processing content lists, its loop traversal tagforplays a core role. When we usearchiveListTag to obtain the list of articles and utilizeforWhile looping through these articles, the system will provide an embedded one for each iterationforloopAn object, this object contains some very practical properties, including the judgment of the current loop positionCounter.

forloop.CounterThis property will start counting from 1, representing the current iteration of the loop. This means that whenforloop.CounterWhen the value is 1, we can determine that the article we are processing is the first article in the list.

Use this simple judgment, and we can apply specific styles or structures to the first article in the template.For example, we can add a special CSS class to it to differentiate its style from the subsequent articles;Or, we can directly render a completely different HTML structure to achieve more diverse display effects.

The following is a specific example of how toarchiveListLoop and highlight the first article:

Assuming we want the first article in the list to have a more prominent title style and a special background, while subsequent articles use the standard display.

{% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
        <div class="article-item {% if forloop.Counter == 1 %}featured-article{% endif %}">
            {% if forloop.Counter == 1 %}
                <h2 class="first-title"><a href="{{item.Link}}">{{item.Title}}</a></h2>
                <img src="{{item.Logo}}" alt="{{item.Title}}" class="first-image">
                <p class="first-desc">{{item.Description}}</p>
            {% else %}
                <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
                <p>{{item.Description}}</p>
            {% endif %}
            <span class="read-count">浏览量:{{item.Views}}</span>
            <span class="publish-date">发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
        </div>
    {% empty %}
        <p>暂无文章内容,请稍后再来。</p>
    {% endfor %}
{% endarchiveList %}

In this code block:

  • We first usearchiveListThe tag got the first 5 articles.
  • Next, inforThrough the loop, by{% if forloop.Counter == 1 %}Determine whether the current article is the first in the list.
  • If it is the first one, we will add an external one for it.divto the elementfeatured-articleThis CSS class, and rendered a<h2>title, onefirst-imagecategory image and onefirst-desccategory description.
  • If it is not the first one, then it is rendered according to the usual<h3>The title and paragraph tags are rendered.

This method allows you to customize the first article in the list according to your actual design needs, whether it is to modify the text content, add additional images, videos, or adjust the overall layout, all of which can be achieved through simple conditional judgments.In this way, the homepage or article list page of the website can show richer layers and better visual guidance effects.

Frequently Asked Questions (FAQ)

1. In addition toforloop.CounterAre there other ways to determine the last, or the second-to-last article in the loop?

Of course. In addition toforloop.Counter(Counting from 1), AnQiCMS'sforloopThe object also providesforloop.Revcounterproperty, it indicates how many iterations are left until the current loop ends (the last element'sRevcounterThe value is 1). For example, to determine if it is the last article, you can use{% if forloop.Revcounter == 1 %}.

2. How can I highlight the first N articles in the list (for example, the first 3 articles)?

You can useforloop.CounterPerform a range judgment. Simply change the condition to{% if forloop.Counter <= 3 %}It can be. In this way, the first three articles in the loop will be considered as "highlighted" articles, and you can apply the same special style or structure to them.

3. What are the main scenarios where this technique of highlighting the first article is applicable?

This skill is very practical in many content operation scenarios.For example, in the article list area on the homepage of the website, you can highlight the latest published or most important headline article to attract the user's attention;On the category list page, you can place the most popular or recommended articles at the top and beautify them;Or in the product display, place the main product or new items in the most prominent position.In this way, it can effectively enhance the importance of content and the user experience.

Related articles

How to display the reading views of articles on the AnQiCMS article list or detail page?

In website content operation, the number of article views is an important indicator, which helps us understand the popularity of the content and the user's interests.AnQiCMS provides a convenient way for you to visually display this data on the article list page or detail page. ### Understanding AnQiCMS's Page View Mechanism AnQiCMS is a comprehensive content management system that comes with a built-in mechanism for automatically recording article page views.

2025-11-08

How to format the display of the publication and update time of articles in the AnQiCMS template?

In Anqi CMS website templates, displaying the publication and update time of articles is a key factor in improving user experience and content management efficiency.Clearly present this time information, which not only helps visitors quickly understand the timeliness of the content, but also assists search engines in better crawling and indexing.AnQiCMS provides a powerful and flexible template tag that allows you to format timestamps into various easily readable date and time formats as needed.### Core Mechanism: `stampToDate` tag The template system of Anqi CMS is built-in with a named

2025-11-08

How to automatically generate and display a table of contents (TOC) on the AnQiCMS article content page?

In AnQiCMS, automatically generating and displaying the table of contents (TOC) for article content pages not only significantly enhances the user reading experience but also helps search engines better understand the article structure, thereby having a positive impact on SEO.With the powerful and flexible template system of AnQiCMS, it is simpler to achieve this function than you imagine.How does AnQiCMS support automatic directory generation?

2025-11-08

How to build multi-condition filtering function for article list and display results in AnQiCMS?

In website operation, providing flexible multi-condition filtering functions for article lists can greatly enhance user experience and the discoverability of content.AnQiCMS (AnQiCMS) leverages its powerful content model and rich template tags to make building such features intuitive and efficient.Below, we will delve into how to step by step implement multi-condition filtering and display results of article lists in AnQiCMS.

2025-11-08

How to nested display the list of articles under the category page in AnQiCMS?

In website operation, the content classification page carries the important responsibility of organizing information and guiding users to browse.A well-designed and content-rich category page can not only improve user experience but also optimize search engine crawling efficiency.For users of AnQiCMS, nesting the list of articles under the category page is a very basic and commonly used feature.This article will introduce you in detail on how to use the powerful template tags of Anqi CMS to easily achieve this goal.Understanding the Classification Page and Content Model Before we begin

2025-11-08

How to filter and display a specific list of articles based on recommended properties (Flag) in AnQiCMS?

An QiCMS provides operators with rich tools for organizing and displaying website content with its flexible and efficient content management capabilities.Among the "recommended attributes" (Flag) is a very practical feature that allows you to easily categorize and mark articles, thereby enabling accurate content filtering and dynamic display on the website front end.This article will introduce in detail how to use these recommended attributes in AnQiCMS to filter and display the specific article list you want.### Deep Understanding of AnQiCMS Recommendation Attributes (Flag) AnQiCMS Recommendation Attributes

2025-11-08

How does AnQiCMS display the list of all top-level categories?

In website construction and content operation, clear classification navigation is the foundation of user experience, as well as the core of content organization.For users using AnQiCMS, how to efficiently display the list of all top-level categories of the website is the first step in building the website structure.AnQiCMS with its flexible template engine and rich built-in tags makes this task exceptionally simple and intuitive.

2025-11-08

How to display the subcategory list under the AnQiCMS category page?

In AnQiCMS, you can easily display the subcategory list under the category page with flexible template tags.This is crucial for building a clear website structure, improving user navigation experience, and optimizing search engine crawling efficiency.AnQiCMS powerful template system and its Django-style tag syntax make the display of such dynamic content intuitive and efficient.To implement displaying the subcategory list under the category page, the following steps are mainly involved:

2025-11-08