In managing website content in Anqi CMS, we often need to make the category page not just a simple aggregation of document lists, but also able to carry more exclusive information to better guide users or display the characteristics of the category.This is particularly important for adding and retrieving custom fields for classification.Through these custom fields, you can make each category page unique, whether it's displaying specific Banner images, contact information, or the unique properties of that category.

Understanding the importance of category custom fields

The AutoCMS provides powerful content models and category management features, allowing us to define various custom fields for content (such as articles, products) to meet different business needs.Similarly, the classification itself supports adding these personalized custom information.This means that in addition to basic attributes such as category name, description, etc., you can also set custom fields such as 'Category Banner Large Image', 'Unique Contact Information for This Category', and 'Special Tag Description' in the background according to your needs.

These fields, once set, are like unique 'seals' for the categories, greatly enriching the expressiveness of the category page.For example, if you operate a travel website, you can add a dropdown field for 'Travel Season' under the 'Island Trip' category, or set a numeric field for 'Family Trip' category with 'Recommended Age Group'.So, when users visit these category pages, they can get more accurate and practical information at a glance.

Get custom fields on the category detail page

We need to use the built-in Anqi CMS template to display these custom fields on the category detail page.categoryDetailLabel.This tag is the key to getting detailed information about categories, it not only helps us get basic data such as category ID, title, link, and description, but also allows us to access the custom fields defined for categories in the background through a special parameter.

When you add custom fields for categories in the background (usually by editing a category in 'Category Management' and setting it in the 'Other Parameters' or 'More Model Custom Fields' section), these fields will be encapsulated in a CMS namedExtraThe collection of data.

To get and iterate over all custom fields, you can use the template on the category details page (usually){分类模型}/list.htmlor the custom template file you specified for the category), and use it in this way.categoryDetailTags:

{% categoryDetail categoryExtraFields with name="Extra" %}
{% if categoryExtraFields %}
    <div class="category-custom-info">
        <h3>分类附加信息</h3>
        {% for field in categoryExtraFields %}
            {# 仅显示有值的自定义字段 #}
            {% if field.Value %}
                <p><strong>{{ field.Name }}:</strong> {{ field.Value|safe }}</p>
            {% endif %}
        {% endfor %}
    </div>
{% endif %}

In this code block:

  • {% categoryDetail categoryExtraFields with name="Extra" %}We defined a variablecategoryExtraFieldsThis stores all the custom fields of the current category.name="Extra"This indicates that we are getting these additional custom data.
  • {% if categoryExtraFields %}This is a simple judgment, ensuring that the content below is rendered only when the category has custom fields, to avoid unnecessary empty modules on the page.
  • {% for field in categoryExtraFields %}Due toExtraReturns an array or list containing multiple custom fields, we need to usefora loop to iterate over them one by one.
  • {{ field.Name }}:Will output the 'parameter name' (such as '**Travel Season') you set for the custom field in the background.
  • {{ field.Value|safe }}:Will output the specific value of this custom field (for example, "Spring and Autumn"). Here,|safeThe filter is very important, especially when the content of your custom fields may contain HTML rich text. It ensures that the content is correctly parsed and displayed as intended, rather than being output as plain text.

If you know the "custom field name called" (i.e., the lowercase English field name used when defined in the background, such asbestSeason), you can also directly throughcategoryDetailTo get this specific field value without traversing all fields:

{% categoryDetail with name="bestSeason" %}

This allows you to directly locate and display the content of a specific custom field in the template, which is very convenient when you only need to display a specific piece of information without listing all custom fields.

Practice Tips

  1. Basic Back-end Settings:All custom fields must be selected first in the "Content Management" -u003e "Document Category" section of the Anqi CMS backend, and then enter the editing interface.In the "Other Parameters" or "More Model Custom Fields" area, add and configure these fields according to your needs.Please note that the "parameter name" defined here is for the background administrator to view, while the "field name" is the name you use directly in the template to get a specific field.
  2. Flexible Use of Field Types:The CMS supports various custom field types, such as single-line text, multi-line text, numbers, single selection, multiple selection, and dropdown selection.Choose the appropriate type based on the actual use of your custom field, which will affect its input form in the background and the presentation of the value on the front end.
  3. Conditional Judgment Is Essential:When displaying custom fields in templates, it is recommended to add conditional judgment (such as{% if field.Value %}To avoid blank labels or unattractive prompts on the page when a field is not filled in.
  4. Note|safeThe use of:If your custom field content may contain HTML tags (for example, if you are using a rich text editor), please be sure to use|safeFilter, otherwise the HTML code will be displayed as plain text.

Through these flexible custom field and template call methods, Anqi CMS makes your category detail page more dynamic and informative, thus better serving your website operation goals.


Common Questions (FAQ)

1. Why am I trying to get custom fields on the category detail page, but no content is displayed on the page?Firstly, please check if you have indeed added custom fields in the "Document Category" editing page of the AnQi CMS backend, and that these fields have actual values. Secondly, confirm that you are using the correctcategoryDetailLabel, especiallyname="Extra"Parameter, and looped throughcategoryExtraFieldsVariable. If you directly access a specific field, ensurenameThe value of the parameter exactly matches the 'Called Field Name' custom field defined in the backend and is spelled correctly.

**2. I can directly access a specific custom field instead of iterating over all