As a senior Anqi CMS website operation personnel, I know that standard fields often cannot meet the increasingly personalized needs of website content display.The introduction of custom parameter fields provides us with great flexibility, allowing us to add various unique attributes to articles based on different content models, thus enriching the content dimension and enhancing the user experience.
This article will elaborate on how to effectively display the custom parameter fields of the article detail page in Anqi CMS.This involves not only the invocation of front-end templates, but also requires a clear understanding of the backend content model and the article editing process.
Understand the meaning of custom parameter fields
In the daily operation of AnQiCMS, we often encounter the need to add some specific information to articles beyond standard fields, such as product articles may require 'product model', 'brand', 'origin' etc., while blog articles may require 'author biography', 'reading time', 'field of expertise' etc.This information cannot be simply placed in the article title or正文, they need to be structured for users to quickly obtain and filter.The custom parameter field is born for this, allowing us to define a set of exclusive additional fields for each content model, greatly enhancing the expressiveness and scalability of content.
Define custom parameter field: basic backend configuration
To make the article detail page display custom parameters, you first need to complete these field definitions in the AnQiCMS backend.The strength of AnQiCMS lies in the flexibility of its content model, which allows us to create or modify the structure of different content types according to business needs.
In the backend management interface, you need to navigate toContent Managementand then selectContent model. Here, you can choose to edit an existing content model (such as an 'article model' or a 'product model'), or create a new one.
After entering the editing page of a specific content model, you will see a namedCustom fields for content modelThe area. Click to add and start defining a new custom parameter field. When adding a field, several key pieces of information need to be clearly filled in:
- Parameter NameThis is the field name displayed in the background management interface and front-end template, such as "Product Model", "Author".
- Field invocationThis is the unique identifier used to call the field value in the front-end template. It is strongly recommended to use short, descriptive letters (such as
model_number/author), and it maintains its uniqueness throughout the website, which will directly affect whether you can correctly call the data in the template. - Field type: AnQiCMS provides various field types, such as single-line text, numbers, multi-line text, single choice, multiple choice, dropdown selection, and so on.Choosing the appropriate field type helps ensure data accuracy and content consistency.
- Mandatory?According to the importance of the field, you can choose whether it is required.
- Default value: You can set a default value for the field, which will be displayed if the field is not filled in when editing the article. Here, it is used to define all the possible values for the selection type field.
After completing the definition of custom fields and saving the content model, these fields are ready for subsequent article content entry and frontend display.
Fill in the custom parameter field: content entry practice
Once custom fields are defined in the content model, they will automatically appear on the article editing page of the corresponding model. When we useContent ManagementEnterPublish documentor when editing an existing article, first you needSelect the category it belongs to.. Because each category is associated with a specific content model, the system will only load the custom fields defined under the content model of the selected category.
Below the article editing page, you will find a named.The foldable box. Expand this foldable box, and you will see all the custom parameter fields defined for the current content model of the article.You only need to fill in or select the corresponding value according to the actual content.Make sure all necessary information is entered accurately, as this will directly affect the display effect of these parameters on the front-end page.
Custom parameters: template calling techniques are displayed on the article detail page
This is the core step to implement custom parameter fields for front-end display on the website.AnQiCMS template system is based on Django template engine syntax, providing flexible tags to retrieve and render data.The template file for the article detail page is usually located{模型table}/detail.htmlor{模型table}/detail-{文档ID}.htmlpath.
To display custom parameters in these template files, we mainly usearchiveDetailandarchiveParamsthese two template tags.
Method one: Directly call the specific custom field
If you know the custom field调用字段Name, and you only need to display a few specific fields, you can usearchiveDetailLabel directly retrieves its value. This method is concise and clear, suitable for scenarios where the number of fields is not many and the display position is fixed.
For example, if you define a调用字段WithauthorThe custom field to display the article author can be called like this in the template:
<div>文章作者:{% archiveDetail with name="author" %}</div>
If this custom field may contain HTML content (such as some rich text editing introductions), in order to prevent HTML from being escaped, it is necessary to use|safeFilter:
<div>作者简介:{% archiveDetail archiveAuthorBio with name="author_bio" %}{{archiveAuthorBio|safe}}</div>
For custom fields of image type, for example, if a field namedcover_imageis defined, the URL of the field can be used assrcattribute:
<img src="{% archiveDetail with name="cover_image" %}" alt="文章封面">
If the custom field stores a set of images (such as a multi-image fieldarcimages), then you need to retrieve the data to a variable first and then throughforLoop through and display:
{% archiveDetail arcimages with name="arcimages" %}
<div class="product-gallery">
{% for img in arcimages %}
<img src="{{img}}" alt="产品图片">
{% endfor %}
</div>
Method two: Loop through all custom fields
When you want to dynamically display all custom parameters, or when the number and type of custom fields may change, usearchiveParamsThe tag loop traversal is a more flexible choice. It can retrieve the list of all custom fields of the current article and then display them one by one in a loop.
archiveParamsThe label defaults to returning a sorted array of objects, each containingName(the parameter name of the field) andValue(the value of the field).
<div class="article-meta-parameters">
{% archiveParams params %}
{% for item in params %}
<div class="parameter-item">
<span class="parameter-name">{{item.Name}}:</span>
<span class="parameter-value">{{item.Value}}</span>
</div>
{% endfor %}
{% endarchiveParams %}
</div>
This method is particularly suitable for product parameter lists, technical specifications, and other scenarios where a unified format is needed to display all additional information. You can also according toitem.NamePerform conditional judgment, perform special processing on specific fields. For example, if the value of a field is an image link, you can render it as<img>Label; if it is a checkbox, it may need to be further processedValueComma-separated string
Practical suggestions
- Naming convention: When defining custom parameter fields, make sure
调用字段The naming should be descriptive and easy to remember, and try to avoid using AnQiCMS built-in field names to prevent conflicts. - Front-end Style: When displaying custom parameters in the template, be sure to combine them with CSS styling to beautify them, ensuring they are consistent with the overall design style of the website and provide a good visual experience.
- Conditional judgmentFor optional custom fields, when calling in the template, you can use
{% if 字段名 %}to make conditional judgments, to avoid displaying empty content when the field has no value, which affects the neatness of the page. - Avoid overuseAlthough the custom parameter function is powerful, overly complex field settings may increase the burden on content managers and may have an impact on database performance