In a content management system, effectively organizing and displaying content is the key to improving user experience and website SEO performance.AnQiCMS provides powerful and flexible template tags, allowing developers to easily build rich feature page layouts.Where, calling the category list and displaying the number of articles under it is a very common and practical need in website navigation and content overview.This not only helps users quickly understand the content volume of each category, but also provides clear website structure information for search engines.
Understanding the core function of the `categoryList` tag
In the template system of AnQiCMS,categoryListLabels are the key tools for retrieving the website classification list.By this tag, we can flexibly extract the classification information we need according to different requirements.categoryListCan handle everything easily.
When usingcategoryListWhen labeling, you will find that it supports some important parameters to accurately locate the required category:
moduleIdThis is a very important parameter that allows you to specify the category of content model to retrieve. For example, if you want to list all categories of the 'article' type, you can setmoduleId="1"(Assuming the article model ID is 1). Similarly, product categories may correspond tomoduleId="2". This way, you can ensure that only categories related to specific content types are displayed.parentIdThis parameter is used to specify the ID of the parent category. When set toparentId="0"when, it will return all top-level categories. If you want to get the subcategories under a specific category, just set the category's ID asparentIdthe value.
categoryListThe label execution will return an array containing multiple category objects. We usually assign a variable name to this array, such ascategoriesThen loop through these category objects to display them.
Easily get the number of articles under a category: the `ArchiveCount` field.
The convenience of AnQiCMS template tags lies in the fact that when you usecategoryListWhen getting the category list, each returned category objectdirectly contains a field namedArchiveCount.This field does not require additional queries; it will automatically count and store the total number of articles associated with this category (and its subcategories, if any).childThe parameter defaults totrueThen) the total number of articles associated with this category (and its subcategories).
This means that you can directly access when traversing the category listitem.ArchiveCountRetrieve and display the number of articles under each category without the need for complex secondary data processing. This greatly simplifies the development of templates and improves efficiency.
Practical Code Examples: Displaying Category Lists and Article Counts
Now, let's demonstrate how to implement this feature with specific code snippets.
Example one: Display all top-level article categories and their article counts
Assume your website has multiple top-level article categories, and you want to display these category names in the sidebar or main navigation, clearly indicating how many articles each category currently contains.
<nav class="category-navigation">
<h3>探索我们的文章分类</h3>
<ul class="category-list">
{% categoryList categories with moduleId="1" parentId="0" %}
{% for item in categories %}
<li>
<a href="{{ item.Link }}" title="查看{{ item.Title }}下的所有文章">
{{ item.Title }}
<span class="article-count">({{ item.ArchiveCount }} 篇文章)</span>
</a>
</li>
{% empty %}
<li>暂无分类内容。</li>
{% endfor %}
{% endcategoryList %}
</ul>
</nav>
This code first usescategoryListtags to getmoduleIdfor '1' (usually representing an article model) andparentIdFor the category "0" (i.e., top-level category). Then, it will traverse these categories, generate a link for each category, and display its title (item.Title) and the number of articles (item.ArchiveCount).item.LinkIt will automatically generate the access link for the category. If there are no categories,emptysome content will be displayed.
Example two: Display multi-level category structure and count the number of articles at each level
categoryListTags also support this kind of scenario well.
<div class="multi-level-categories">
<h2>内容导航</h2>
{% categoryList topCategories with moduleId="1" parentId="0" %}
<ul class="main-categories">
{% for topItem in topCategories %}
<li>
<a href="{{ topItem.Link }}">{{ topItem.Title }}</a>
<span class="count">({{ topItem.ArchiveCount }} 篇文章)</span>
{% if topItem.HasChildren %} {# 判断当前分类是否有子分类 #}
<ul class="sub-categories">
{% categoryList subCategories with parentId=topItem.Id %}
{% for subItem in subCategories %}
<li>
<a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
<span class="count">({{ subItem.ArchiveCount }} 篇文章)</span>
{# 如果还需要更深层级,可以继续嵌套 categoryList #}
</li>
{% endfor %}
{% endcategoryList %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
{% empty %}
<p>没有找到任何分类。</p>
{% endcategoryList %}
</div>
In this example, we first get the top-level category (topCategories). While traversing each top-level category, we utilizetopItem.HasChildrenThis boolean value is used to determine if the category has subcategories. If it exists, we will use it again.categoryListLabel, and use the ID of the current top-level category asparentIdto retrieve and display its subcategories.subCategoriesIt also displays the number of articles they have. This structure can be flexibly expanded to any depth, as long as your category system supports it.
The value considerations of content operation
Clearly displaying categories and the number of articles on a website has multiple values:
- Enhance user experience:Users can intuitively see the richness of content in each category while browsing the website, which helps them quickly locate topics of interest and reduce the time spent on blind exploration.
- Optimize content discovery:For new users or those visiting a specific page for the first time, the number of articles provides an expectation of content depth, encouraging them to delve deeper into browsing.
- Support SEO strategies:A clear classification structure and content quantity suggest the breadth and depth of the website's content, which is very beneficial for search engines to understand the website's theme and to crawl and index it.At the same time, the number of articles on the category page can be used as a reference for page weight distribution.
- Guide content planning:The operations team can judge which categories have relatively weak content based on the number of articles in each category, thereby carrying out targeted content supplementation and optimization.
The template tag design of AnQiCMS fully considers these operational needs, simplifying complex data calls into intuitive and easy-to-use tags, making the organization and display of website content unprecedentedly convenient. By using flexiblycategoryListandArchiveCountTags such as these can provide users with a better browsing experience and lay a solid foundation for the long-term development of the website.
Common Questions (FAQ)
1. If there are no articles under a certain category,ArchiveCountWhat will be displayed?
When there are no related articles under a category,ArchiveCountthe field will naturally display as0This means you do not need any additional conditional judgment to handle the case of empty categories, the template will output zero directly, which is also very clear to the user.
2. In addition to the number of articles,categoryListWhat information can the tag obtain about the categories?
categoryListEach category object returned by the tag is very rich. Besides,Id/Title/LinkandArchiveCountyou can also access,Description(category introduction),ParentId(parent category ID),Logo(Category Large Image),Thumb(Category thumbnail),HasChildren(Whether there are subcategories) and various other information. This allows you to have a high degree of freedom when designing category displays.
3. Can I retrieve categories under a specific content model without affecting other models?
Of course you can.categoryListTagsmoduleIdThe parameter is set for this purpose.moduleId="您内容模型的ID"You can accurately specify to only retrieve the category lists under the article, product, or other custom content models, without confusing different types of content. For example, if you want to display only product categories on a certain page, you can setmoduleIdSet the ID of the product model.