In website operation, displaying the latest published content to visitors in a timely manner can not only effectively improve user experience, but also enhance the vitality of the website.AutoCMS (AutoCMS) provides an intuitive and powerful template tag feature, allowing you to easily display the latest article list and its publication time on the website front-end.
Understanding the article display mechanism of Anqi CMS
The website data is called and displayed using template tags in AnQi CMS.This means that you do not need to write complex backend code, just use predefined tags in the template file, and the system can automatically extract and present the required content from the database.archiveListTags andstampToDateTime formatting tags.
核心步骤:调用最新文章列表与发布时间
要在前台显示最新的文章列表和它们的发布时间,我们可以遵循以下几个简单步骤:
Confirm the source of the article and sorting methodFirstly, we need to inform the CMS that we developed that which articles to display and how to sort them.
archiveListTags are the key to obtaining the list of articles.- Specify the content model (
moduleId)In the Auto CMS, articles, products, and other items are classified under different "content models". Usually, the content model ID for articles is 1. Therefore, you can setmoduleId="1"Ensure that we are getting the article. - Specify the sorting order (
order)To display the latest articles, we need to sort them in descending order by their publication time (usually corresponding to the ID or creation time in the database).order="id desc"It is a commonly used method, which sorts the articles by article ID in descending order. Since the ID is auto-incremented, this is usually equivalent to sorting by publication time from new to old. - Limit the number of displayed items (
limit):You can limit the number of articles displayed in the list according to your page design needs, for example.limit="10"It will display the latest 10 articles. - List type (
type):For regular list display, it is usually set totype="list".
- Specify the content model (
Use
archiveListLabel data acquisitionIn your template file (such as the homepageindex.htmlor article list pagearchive/list.htmletc.), usearchiveListtag to wrap your article list structure.{% archiveList archives with moduleId="1" order="id desc" limit="10" type="list" %} {# 文章列表将在这里循环显示 #} {% endarchiveList %}Here, the article list obtained is assigned to a variable named
archives.Loop output the article title, link, and publish time
archiveListTags will return an array of article objects, you can useforLoop through this array and extract the details of each article.- Article title: Through
{{item.Title}}Show article title. - Article link.: Through
{{item.Link}}To get the article detail page link. - publication time: The creation time of the article is stored in
item.CreatedTimeField, this is a timestamp. To display it in a readable date format, we need to usestampToDatetag for formatting, for example{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}It will be formatted into the form of "year-month-day hour:minute."
- Article title: Through
Complete code example:
Place the following code snippet in the template location where you want to display the latest article list, such as a block or sidebar file on the home page of the website:
<div class="latest-articles-list">
<h3>最新文章</h3>
<ul>
{% archiveList archives with moduleId="1" order="id desc" limit="10" type="list" %}
{% for item in archives %}
<li>
<a href="{{ item.Link }}" title="{{ item.Title }}">
<span class="article-title">{{ item.Title }}</span>
<span class="article-date">{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span> {# 显示年-月-日 #}
</a>
</li>
{% empty %}
<li>暂无最新文章。</li>
{% endfor %}
{% endarchiveList %}
</ul>
</div>
This code will generate an unordered list containing the latest 10 articles.Each article displays its title and publication date.If there are no articles, it will display 'No latest articles available.'The prompt.
The location to place the template code
This code can be flexibly placed in any template file of your website. For example:
- Home page (
index.html): Usually used to display selected content of the website. - Sidebar (
partial/sidebar.html)In a blog or information station, the sidebar is often used to display popular articles, latest articles, etc. - Specific category page (
archive/list.html)The latest articles in a category can be highlighted at the top or bottom of a category page.
Through the above steps, you can easily display the latest article list and its publication time on the front end of the website built with Anqi CMS, providing fresh and valuable content to visitors.
Common Questions (FAQ)
Q: I only want to display the latest articles under a specific category, what should I do?A: You can in
archiveListtag.categoryIdParameter to specify the article category. For example, if your category ID is 5, you can write it like this:{% archiveList archives with moduleId="1" categoryId="5" order="id desc" limit="10" type="list" %}.Q: Not satisfied with the display format of the publish time? How to adjust?A:
stampToDateThe second parameter of the label is the format string. You can adjust it as needed. For example,"2006年01月02日"will be displayed as “2006-01-02”,"15:04"The display will show "15:04" (time only). Please refer to the Go language's formatting rules for setting the time format.Q: If the article list is long, how can I implement pagination?A: If you need to display pagination, you can
archiveListTagstypeparameter settings"page"then combinepaginationuse tags together. For example:{% archiveList archives with moduleId="1" order="id desc" limit="10" type="page" %}… (文章列表循环) …{% pagination pages with show="5" %}(分页链接){% endpagination %}This will generate pagination navigation below the article list.