How to use the `languages` tag to build a language switch menu and output the `hreflang` tag in a multilingual site?

Calendar 👁️ 74

Building multilingual sites in AnQiCMS (AnQiCMS) not only expands your market reach but also provides a more friendly access experience for users in different regions. The key to achieving this goal lies in effective utilizationlanguagesCreate a language switch menu tag and output correctlyhreflangOptimize search engine recognition with the tag.

AnQiCMS is a content management system that focuses on enterprise-level applications, and its powerful multilingual support is one of its highlights.It allows you to create multiple language versions of website content and provides convenient tools to manage this content.languagesThe tag, which is the key to presenting the multi-language capabilities configured on the backend to the website front end.

Core Tool:languagesThe use of tags

AnQiCMS provides a built-in tag specifically for handling multi-language site information——languages. This tag is designed to retrieve the list of all multilingual sites configured on your website. It is a direct and powerful tool,does not accept any parametersIt will default to retrieve all multi-language site information set and enabled in the background, making it very simple to use.

When you use it in the template{% languages websites %}...{% endlanguages %}then,websitesThe variable will become an array object containing details of each language version site. You can access the specific data of each language site by looping through thiswebsitesarray.

EachitemAn object (i.e.,websiteseach element in the array provides the following useful fields:

  • LanguageName: The language name of the site (e.g., “Simplified Chinese”, “English”).
  • Language: Language's ISO code (e.g.)zh-CN,en-US),This ishreflangtags are crucial.
  • LanguageEmoji: An emoji representing the language, used for visual language switching.
  • LanguageIcon: Custom language icon URL, typically a small flag image.
  • Name: Display name of the site.
  • Link: Full URL of the site in the language version.
  • RemarkSite remark information.
  • NofollowWhether to addnofollowProperty.

UnderstoodlanguagesAfter getting the information that tags can provide, we can start building the language switching function.

Build the language switching menu

To facilitate users switching between different language versions, a user-friendly and easily accessible language switch menu is crucial. Uselanguagestags, you can flexibly design the menu style.

This is a basic language switch menu code example, you can place it in a prominent position such as the header, footer, or sidebar of a website.

{%- languages websites %}
{%- if websites %}
<div class="language-switcher">
    <span>选择语言:</span>
    {%- for item in websites %}
    <a href="{{item.Link}}" class="lang-option">
        {%- if item.LanguageIcon %}
        <img src="{{item.LanguageIcon}}" alt="{{item.LanguageName}}"/>
        {%- else %}
        {{item.LanguageEmoji}}
        {% endif %}
        {{item.LanguageName}}
    </a>
    {%- endfor %}
</div>
{%- endif %}
{%- endlanguages %}

In this code block:

  • We first use{%- languages websites %}Get the list of all language sites.websitesThe variable will contain information about all language versions.
  • {%- if websites %}Check if there are any available language sites to avoid displaying an empty menu when multilingual is not configured.
  • {%- for item in websites %}Loop through each language site.
  • item.LinkProvided is the complete URL pointing to the language version site, ensuring that users can click and be redirected correctly.
  • You can chooseitem.LanguageIconto display a custom flag icon, or useitem.LanguageEmojiDisplay a more concise Emoji表情, or display directlyitem.LanguageName(Language name). This flexibility allows you to adjust according to the design style of the website.

With such a menu, your users can easily switch between different language versions of the website, greatly enhancing the user experience.

outputhreflangTags for optimizing SEO

In addition to the visible language switch menu, Search Engine Optimization (SEO) also requires us to clearly inform search engines of the corresponding relationships between different language versions. At this point,hreflangthe label comes into play.hreflangTags help search engines understand the geographic or language location of your website content, avoid duplicate content in multilingual content, and ensure that the correct content version is presented to search users in different languages or regions.

hreflangLabels are usually placed on each page<head>in the area, its standard format is<link rel="alternate" href="[URL]" hreflang="[语言代码]"Combined.languageslabels, you can easily generate these labels for each page:

<head>
    {# 其他head标签... #}
    {%- languages websites %}
    {%- for item in websites %}
    <link rel="alternate" href="{{item.Link}}" hreflang="{{item.Language}}">
    {%- endfor %}
    {%- endlanguages %}
</head>

In this code block:

  • item.LinkIt provides the complete URL for the current page language version. Please note thatitem.Linkit will automatically point to the language version URL of the current page, AnQiCMS will intelligently match.
  • item.LanguageThe corresponding language code is provided (for exampleen/zh-CN/fretc.), which ishreflanga required attribute of the tag
  • After these tags are generated, they will be placed on each page<head>In the region, make sure that the search engine can correctly identify and index the multilingual content of your website.

Proper configurationhreflangTags are crucial for multilingual SEO. They not only help improve the visibility of your website globally but also avoid SEO penalties due to similar content in language versions.

Implementation and precautions

EnsurelanguagesThe tag works correctly, you first need to configure and manage the multilingual site in the AnQiCMS backend's "Backend Settings" -> "Global Settings". Only the language versions enabled in the backend will belanguagesThe label is obtained. In addition, setting the 'Default Language Package' in 'Global Settings' ensures that the system interface and some built-in prompts are displayed in the selected language.

By combining usinglanguagesLabel-building user-friendly language switch menu, as well as search engine-orientedhreflangLabel, not only can you significantly enhance the user experience, but you can also effectively optimize the global SEO performance of the website, helping your content reach a wider audience.AnQiCMS provides all the necessary tools to make the operation of multilingual websites simple and efficient.


Frequently Asked Questions (FAQ)

1. ConfigurationlanguagesWhy does the language switch menu or label not display any content afterhreflangthe label?This is usually because you have not configured or enabled any multilingual sites in the AnQiCMS backend.languagesThe tag will retrieve all language sites that are set and enabled under 'Multi-site Management' in the 'Global Settings' on the backend.Please check your backend configuration to ensure that at least one language site other than the default site has been added and activated.

**2.hreflangin the labelhreflangThe attribute value (i.e.,item.Language) should be filled out in accordance with how

Related articles

How to dynamically output the SEO title, keywords, and description (TDK) on AnQiCMS templates?

The performance of a website in search engines directly affects the efficiency of content access, and the title (Title), keywords (Keywords), and description (Description), abbreviated as TDK, are the 'business card' for search engines to understand page content.Effectively manage and dynamically output TDK, which can significantly improve the SEO effect and click-through rate of the website.AnQiCMS is a content management system that highly values SEO and provides flexible and powerful features, making it easy to dynamically configure page TDK.

2025-11-08

If the article content is in Markdown format, how to use the `render` filter to render it as HTML?

AnQi CMS is favored by many users for its efficient and flexible content management capabilities.In content creation, Markdown format has become the preferred choice for many content creators due to its simplicity and efficiency.But how to beautifully present these Markdown-formatted contents as structured HTML on the website front-end is a concern for many users.

2025-11-08

How to ensure that HTML code is correctly parsed and not escaped when displaying rich text content using the `safe` filter?

In AnQi CMS, rich text content usually carries articles, product descriptions, or page details with rich information. This content often includes various HTML tags such as bold, italic, images, links, tables, and so on.If this HTML code is not parsed correctly and is simply displayed as plain text, then what the user sees on the front end is a pile of code, rather than beautiful and structured content. This clearly has a negative impact on the readability and professionalism of the website.

2025-11-08

How to use a filter to automatically find URLs in text and convert them into clickable HTML links?

In website operation, we often encounter such needs: in the content or description of the article, some URLs or email addresses may be included, but they are just plain text, and users cannot click to access directly.Manually adding HTML links to each URL is inefficient and prone to errors, especially when the volume of content is large.AnQiCMS (AnQiCMS) understands this pain point and provides powerful built-in filters to help us easily automate the conversion of URLs in text, making website content more friendly and convenient.### Automated Link

2025-11-08

How to get and display the system parameters configured in the global settings of the AnQiCMS template?

In the AnQiCMS template, efficiently obtaining and displaying the system parameters configured in the background "Global Settings" is the key to ensuring unified website information and convenient management.AnQiCMS provides a set of intuitive and powerful template tags, allowing developers and operators to easily achieve this goal without delving into complex programming code.

2025-11-08

How to obtain the contact information, phone number, email, social media, and other information configured in the background "Contact Settings"?

AnQiCMS provides a convenient function for website administrators to centrally manage and display contact information.The contact information, phone number, email, social media and other information configured in the "Contact Settings" backstage can be flexibly displayed on the website front end through simple template tags, greatly improving the efficiency and consistency of content updates. ### Understand the "Contact Information Settings" on the backend First, let's get familiar with the location of this information in the AnQiCMS backend configuration.

2025-11-08

How to dynamically build and display custom form fields for leaving messages in the template?

In website operation, the message form is an important bridge for users to interact with the website, collect user feedback, or potential customer information.A flexible and customizable feedback form can greatly enhance the practicality and user experience of a website.AnQiCMS (AnQi CMS) provides strong support in this regard, allowing users to customize message form fields in the background and dynamically build and display these fields in the front-end templates, thereby meeting various personalized business needs.### AnQiCMS留言表单的灵活性 AnQiCMS

2025-11-08

How to integrate captcha functionality in comment or message form to prevent spam?

Websites and article comments are an important bridge for interaction between websites and users, and they can effectively improve the activity and user stickiness of the content.However, these open interactive areas are often targeted by spammers, and a large amount of meaningless or malicious information not only damages the image of the website but may also affect search engine rankings.To effectively curb such problems, integrating a captcha function into the message or comment form is an effective method.A safe CMS as an efficient website building tool, fully considering common challenges in content operation and providing convenient solutions

2025-11-08