In managing content in AnQi CMS, tags (Tag) are an important tool for organizing and categorizing information.It can not only help users quickly find relevant content, but also significantly improve the internal link structure and SEO performance of the website.tagDataList标签便成为了不可或缺的核心功能。
本文将深入探讨如何高效地利用tagDataListGet and paginate documents under a specific tag to optimize website content presentation and user experience.
1. UnderstandingtagDataListThe role of the label
tagDataList
The core value of this tag lies in its ability to provide highly customized content lists and support pagination features, which is crucial for building tag archive pages, recommendation blocks for related content, or any scenario that requires content aggregation by tags.
tagDataListThe basic usage format is as follows:.{% tagDataList 变量名称 with 参数... %}
Among them,变量名称It is the name you customized for the document list you obtained, and you can traverse and display it in the template later.
2. English for Getting Documents under a Specific Tag
To get documents under a specific tag,tagDataListyou need to know the identifier of the target tag, that istagId.
- Use it on the tag detail page:If you are editing a label detail page template (usually
tag/detail.htmlortag/list.html)tagDataListthe label will automatically identify the current pagetagId, at this point you can omittagIdParameter. The system will default to retrieve all documents associated with the current tag. - Specify tags on other pages:If you want to display documents under a specific tag on the homepage, category page, or any other page, you need to manually specify the tag ID through parameters (where
tagId="X"are specified.XIs the unique identifier of the tag in the background).
ExcepttagId,moduleIdThe parameter is also very useful. If you want to display only the documents associated with the tag under a specific model (such as an article model or product model), you canmoduleId="1"(Article model ID is 1) ormoduleId="2"(Product model ID is 2) to further filter.
The following is a simple code example showing how to get the first 10 documents under a certain tag (without pagination):
{# 假设我们想获取ID为5的标签下的所有文章(模型ID为1) #}
{% tagDataList archives with tagId="5" moduleId="1" limit="10" order="id desc" %}
{% for item in archives %}
<div class="document-item">
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
<p>{{ item.Description|truncatechars:100 }}</p>
<div class="meta">
<span>发布于:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
<span>浏览量:{{ item.Views }}</span>
{# 可以在此调用分类信息 #}
<span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
</div>
{% if item.Thumb %}
<a href="{{ item.Link }}">
<img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="document-thumb">
</a>
{% endif %}
</div>
{% empty %}
<p>该标签下暂无相关文档。</p>
{% endfor %}
{% endtagDataList %}
In the above code,itemYesforThe current document object in the loop, it contains the document'sId/Title/Link/Description/CreatedTime/Views/Thumb/CategoryIdThe commonly used fields. You can freely call these fields according to your needs to enrich the display content of the document.orderThe parameter is used to specify the sorting method of the document, for exampleid descRepresenting the reverse order by ID (latest release)views descMeans the reverse order by view count.
3. Implement document pagination display
To implement the pagination display of the tag document list, we need to tagDataListLabel addtype="page"parameters, and combinedpaginationtag to render pagination navigation.
type="page"The parameter tellstagDataListTag, we need a paginated dataset that will automatically calculate the total number of pages and current page data based on.limitParameters (how many items per page) and the current page number.
Next,paginationThe tag is responsible for generating all the links and page number information required for pagination. ItsshowThe parameter can control the maximum number of page number buttons displayed in the pagination.
The following is a combinationtagDataListandpaginationImplementation of pagination code example:
{# 假设仍在标签详情页,tagId和moduleId可省略或根据需要指定 #}
{% tagDataList archives with type="page" limit="10" order="id desc" %}
<div class="tag-document-list">
{% for item in archives %}
<div class="document-card">
<a href="{{ item.Link }}" title="{{ item.Title }}">
{% if item.Thumb %}
<img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="card-thumb">
{% endif %}
<h4 class="card-title">{{ item.Title }}</h4>
<p class="card-description">{{ item.Description|truncatechars:120 }}</p>
</a>
<div class="card-meta">
<span>发布日期: {{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
<span>阅读量: {{ item.Views }}</span>
</div>
</div>
{% empty %}
<p>该标签下暂无文档。</p>
{% endfor %}
</div>
{# 分页导航区域 #}
<div class="pagination-container">
{% pagination pages with show="7" %}
<ul class="pagination-list">
{# 首页链接 #}
{% if pages.FirstPage %}
<li class="page-item {% if pages.FirstPage.IsCurrent %}active{% endif %}">
<a href="{{ pages.FirstPage.Link }}" class="page-link">{{ pages.FirstPage.Name }}</a>
</li>
{% endif %}
{# 上一页链接 #}
{% if pages.PrevPage %}
<li class="page-item">
<a href="{{ pages.PrevPage.Link }}" class="page-link">{{ pages.PrevPage.Name }}</a>
</li>
{% endif %}
{# 中间页码 #}
{% for pageItem in pages.Pages %}
<li class="page-item {% if pageItem.IsCurrent %}active{% endif %}">
<a href="{{ pageItem.Link }}" class="page-link">{{ pageItem.Name }}</a>
</li>
{% endfor %}
{# 下一页链接 #}
{% if pages.NextPage %}
<li class="page-item">
<a href="{{ pages.NextPage.Link }}" class="page-link">{{ pages.NextPage.Name }}</a>
</li>
{% endif %}
{# 尾页链接 #}
{% if pages.LastPage %}
<li class="page-item {% if pages.LastPage.IsCurrent %}active{% endif %}">
<a href="{{ pages.LastPage.Link }}" class="page-link">{{ pages.LastPage.Name }}</a>
</li>
{% endif %}
</ul>
<div class="pagination-info">
总计 {{ pages.TotalItems }} 条文档,共 {{ pages.TotalPages }} 页,当前第 {{ pages.CurrentPage }} 页。
</div>
{% endpagination %}
</div>
{% endtagDataList %}
In the pagination code,pagesThe variable contains all pagination information, such aspages.TotalItems(Total number of documents),pages.TotalPages(Total number of pages),pages.CurrentPage(current page number) etc. The most commonly used ispages.Pagesarray, which contains all the page numbers that need to be displayedpageItemobjects, eachpageItemin my template directory.NamePage number text,.Link(Page link), andIsCurrentWhether it is the current page, etc., properties, convenient for us to build flexible pagination navigation.
4. Practical Application and Optimization Suggestions
- SEO Optimization:The label archive page is an important entry for search engines to crawl and index website content. Through
tagDataListDisplay pagination content, which can ensure that each tag has rich indexable content. With the built-in SEO features of Anqi CMS (such as pseudo-static, TDK settings), it can significantly improve the weight and ranking of the tag page in search engines. - User Experience:The clear pagination navigation allows users to easily browse a large number of tag contents without loading all documents at once, which improves the page loading speed and user satisfaction.Organize tag categories reasonably, and cooperate with an aesthetically pleasing template design, so that users can find the information they need more quickly.
- Content Aggregation:The label aggregates related thematic content scattered across different categories on the website, providing users with a completely new path for content discovery.For example, a tag about the 'Go language' can aggregate all articles, tutorials, and related products related to Go language.
- Flexibility:
tagDataListSupport multiple sorting methods (by ID, views, custom sorting, etc.), combined with the richitemField, you can display the most critical information according to your business requirements.
By masteringtagDataListandpagination标签,website operators can better organize and display website content, provide users with a more优质的 browsing experience, and lay a solid foundation for search engine optimization.
Common Questions (FAQ)
Q1:tagIdThe parameter is required? When can it be omitted?
tagIdThe parameter is not always required. When you are on the tag detail page of the security CMS backend (for example)tag/list.htmlortag/detail.htmltemplate) usingtagDataListWhen labeling, the system will automatically identify the tag ID of the current page, at which point the tag ID can be omitted.tagIdHowever, if you want to display documents under a specific tag on the homepage, category page, or any other non-tag detail page, you need to manually through.tagId="X"(where X is the ID of the target label)to specify the target label explicitly.
Q2: Besides the document title and link, what other document information can I display?UsetagDataListThe list of documents obtained (}]archivesEach in the variableitem) Contains very rich document information.