Compatibility restoration practice of WordPress calendar in Polylang multi-language environment

在英文页面 https://www.shuijingwanwq.com/en/ 中,22 号显示蓝色链接

From Classical to Block: Theme Migration

From Hueman to Twenty Twenty-Five, Topic Switching with Multilingual Menu Configuration

(1) From Hueman to Twenty Twenty-Five, Topic Switching with Multilingual Menu Configuration

经过以上步骤,语言切换器最终在页面上的效果符合预期。(见图 9)

(2) In a WordPress 2025 topic, move the Polylang language switch to the full record in the upper right corner

页眉导航宽度异常问题:导航被内容宽度限制(图 4)

(3) WordPress Twenty Twenty-Five Global Width Layout Practical Notes: Widescreen Full-Scale + Large Screen Limited Wide Configuration Scheme

中文(中国)前台首页:66主内容文章+33标准化侧边栏,区块正常显示(对应图6)

(4) Practice|WordPress Twenty-Five Block Theme Text Blog Home Transform the Classic Two-Column Homepage

改造完成最终首页效果(图5)

(5) WordPress Twenty Twenty-Five Two-column homepage transformation: Text Blog Small picture list template complete practical record

图11:样式重写后下拉美观,但层级子分类在原生 Option 标签下以空格缩进表示

(6) Debugging record of the beautification and rendering mechanism of the drop-down menu of the category list

图3:应用修正后的 CSS,日历占据了应有的侧边栏宽度,有文章的日子用主题同色系进行了高亮,悬停时会变黑

(7) Fixed the issue of “getting dissatisfaction” in the sidebar: WordPress 2025 theme calendar style optimization

图5:English 下的页面显示第二个 Language Visibility 区块

(8) Add the multi-language “Personal Brand” block to the sidebar of the blog homepage

图4:调整后的分页效果

(9) Troubleshooting and Repair of One FSE Page Loss: From Pure Layout Template to Query Loop

在英文页面(https://www.shuijingwanwq.com/en/)中,日历上每个日期点击后跳转的链接仍然是 https://www.shuijingwanwq.com/2026/06/08/ 的形式,而不是预期的 https://www.shuijingwanwq.com/en/2026/06/08/。

(10) WordPress 2025 Theme + Polylang: Fix a full record of the missing language directory for the calendar link

图4:中文站点,下拉菜单样式美观,显示“选择年份”。

(11) Optimize WordPress 2025 Themeser: A complete remodeling record of multilingual navigation, social links and archive drop-down bars

图2:分类页单栏效果

(12) From single column to two columns: the practical record of the sidebar and list structure of the WordPress category page unified first page

套用上述代码后,标签云立刻有了质的飞跃:

(13) Say goodbye to uneven! Create a modern label cloud for 2025 themes only with CSS

搜索“alipay”的结果,每篇文章都带了一张大尺寸的特色图片,紧跟着就是完整的正文内容。我的文章里还有代码片段,全都被拉出来显示在列表里,页面无限拉长,排版也乱糟糟的。如图1

(14) Is the search result page too long? I did a “break away” for the WordPress 2025 theme

在英文页面 https://www.shuijingwanwq.com/en/ 中,22 号显示蓝色链接

(15) Compatibility restoration practice of WordPress calendar in Polylang multi-language environment

Network检查确认:如图3

(16) WordPress Theme Migration: Does Emoji Process Code Need to Keep?

图2 Site Wide Header

(17) WordPress Tab NoIndex Optimization: Practical Sharing from Theme Migration to Code Refactoring

最近,我在检查我的 WordPress 网站时,发现浏览器开发者工具的控制台里出现了几个令人不安的红色错误信息:

(18) WordPress Console Error Troubleshooting Transcript: From jQuery conflict to Baidu statistical warning

Introduction

After switching the blog topic from Hueman to Twenty Twenty-Five , I had a tricky problem: the calendar block is showing exceptions in multi-language. in the English page (such as https://www.shuijingwanwq.com/en/), some dates (such as No. 22) on the calendar have not published English articles, but the link is still displayed, and after clicking, it returns to the 404 error page. The Chinese page normally displays the link of the corresponding language. After 1 day of investigation and repair, the problem was finally solved through the PHP code fragment. The process and scheme are now sorted out as follows.

Original question: English page calendar link lacks language prefix

problem description: in the English page https://www.shuijingwanwq.com/en/ , the link that jumps after each date on the calendar is still https://www.shuijingwanwq.com/2026/06/08/ form, not expected https://www.shuijingwanwq.com/en/2026/06/08/. This causes the user to click on the calendar link, due to lack of /en/ The prefix jumps to the error page, which is equivalent to language switching.

Preliminary solution: replace the URL prefix (and new questions)

preliminary

Initially, I tried to solve the original problem by replacing the URL prefix, referring to the blog postFixed the problem of ‘holding dissatisfaction’ in the sidebar: WordPress 2025 theme calendar style optimizationin the solution.

new problems appear

Although the URL prefix problem is temporarily solved, new problems follow: in the English page (https://www.shuijingwanwq.com/en/), the blue link is displayed on the 22nd of the calendar, but the English article is not published on this date, and it will return to 404 after clicking. this showsReplacing URL prefixes alone does not resolve data filtering issues– Calendar still displays article dates in all languages, not only in the current language.

Scenario Comparison: Full Rewrite vs. Modification Based on HTML

For the above problem, I have compared two solutions:

Scenario 1: Completely rewrite the calendar block (the architecture is clear, but it is easy to introduce bugs)

Thinking: Discarded WordPress Native Calendar get_calendar() function, through wp_query Query the article data of the current language, and manually generate the HTML structure.
Advantage:

  • The architecture is clear, and the calendar display is directly controlled from the data layer to avoid potential problems that depend on native functions.
  • The performance is better, because the html needs to be parsed because the data is directly queried.
    Shortcoming: Calendar logic is complex (span month, first day of week alignment, etc.), completely rewrite easy to introduce typesetting bugs, and it is necessary to ensure that it is compatible with the theme CSS, and the maintenance cost is high.

Option 2: Modification based on native HTML structure (preserve native calendar logic, low risk)

Thinking: Let WordPress make the native calendar html, and then pass domDocument Parse and fix:

  1. Query the date collection of articles with articles in the current language for the month.
  2. Iterate over each of the calendar tables <td>, check whether there is an article on the date:
  • There are articles but no links: Create <a> label and add the correct language prefix.
  • No articles but links: remove <a> labels, keep plain text.
  1. Reconstruct the monthly navigation link to ensure that the current language data is generated.
    Advantage: Preserve the layout logic of the native calendar, avoid the introduction of new bugs, compatible theme CSS, and low risk.
    Shortcoming: Additional HTML needs to be parsed, the performance is slightly affected, but acceptable.

Final selection and effect display

Considering the limited time and the theme switching has taken 4 days, I chose Scheme 2 to add PHP code snippets through the WPCode plugin to achieve repair. The following are comparisons before and after the repair:

Before repair: English page No. 22 error display link

In the English page https://www.shuijingwanwq.com/en/ , No. 22 displays a blue link, but no English article is published on this date, and it will return to 404 after clicking.

In the English pagehttps://www.shuijingwanwq.com/en/, No. 22 shows a blue link
In the English page https://www.shuijingwanwq.com/en/ , No. 22 shows a blue link

However, no English article is published on this date, and it will return to 404 after clicking
However, no English article is published on this date, and it will return to 404 after clicking

After repair: English page No. 22 has no link

After the repair, the English page No. 22 only displays plain text, no link, which is in line with expectations.

After the repair, the English page No. 22 only displays plain text, no link, which is in line with expectations.

Plugin Compatibility Considerations: Calendas Plugins

During this period, I considered using the Calendas plugin (polylang is explicitly supported), but it prompts that the PHP version is not compatible (requires 8.2 or higher), and it takes extra time to upgrade PHP, so give up.

During this period, I considered using the Calendas plugin (polylang is explicitly supported), but it prompts that the PHP version is not compatible (requires 8.2 or higher), and it takes extra time to upgrade PHP, so give up.

Code implementation (based on scheme 2)

The following are snippets of PHP code added through the WPCode plugin, the core logic is:

  1. Query the date of the article in the current language that month.
  2. Use domDocument traverse the calendar <td>, to fix the link or remove the wrong link.
  3. Reconstruct the navigation links to make sure the language prefix is correct.
PHP
<?php
function fix_polylang_calendar_dom_patch_v2( $block_content, $block ) {
    if ( 'core/calendar' !== $block['blockName'] || ! function_exists( 'pll_current_language' ) ) {
        return $block_content;
    }
    global $wp_locale;
    $current_lang = pll_current_language( 'slug' );
    $default_lang = pll_default_language( 'slug' );
    $home_url = home_url( '/' );
    // 修正 URL 前缀
    $fix_url = function( $url ) use ( $current_lang, $default_lang, $home_url ) {
        if ( $current_lang === $default_lang ) return $url;
        if ( strpos( $url, $home_url . $current_lang . '/' ) === false ) {
            $relative = str_replace( $home_url, '', $url );
            return $home_url . $current_lang . '/' . $relative;
        }
        return $url;
    };
    // 查询当前语言当月有文章的日期
    $year = get_query_var( 'year' ) ?: (int) gmdate( 'Y' );
    $month = get_query_var( 'monthnum' ) ?: (int) gmdate( 'm' );
    $args = [
        'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1,
        'fields' => 'ids', 'lang' => $current_lang, 'year' => $year, 'monthnum' => $month
    ];
    $query = new WP_Query( $args );
    $valid_days = array_map( 'intval', array_column( $query->posts, 'ID' ) );
    // 解析原生 HTML 并修正
    $dom = new DOMDocument();
    libxml_use_internal_errors( true );
    $dom->loadHTML( '<?xml encoding="utf-8" ?>' . $block_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
    $xpath = new DOMXPath( $dom );
    // 遍历每个日期单元格
    $td_nodes = $xpath->query( '//table[contains(@class, "wp-calendar-table")]/tbody/tr/td' );
    foreach ( $td_nodes as $td ) {
        if ( $td->getAttribute('class') === 'pad' ) continue;
        $day_text = trim( $td->nodeValue );
        if ( ! preg_match('/^\d{1,2}$/', $day_text) ) continue;
        $day_num = (int) $day_text;
        $has_post = in_array( $day_num, $valid_days );
        $existing_a = $xpath->query( './a', $td )->item(0);
        if ( $has_post ) {
            if ( $existing_a ) $existing_a->setAttribute( 'href', $fix_url( get_day_link( $year, $month, $day_num ) ) );
            else {
                $td->nodeValue = '';
                $new_a = $dom->createElement('a', $day_num);
                $new_a->setAttribute( 'href', $fix_url( get_day_link( $year, $month, $day_num ) ) );
                $td->appendChild( $new_a );
            }
        } else {
            if ( $existing_a ) $td->removeChild( $existing_a );
        }
    }
    // 重建导航链接(略,逻辑与正文一致)
    // 输出修正后的 HTML
    $modified_html = $dom->saveHTML();
    return trim( preg_replace( '/<!DOCTYPE[^>]*>|<html[^>]*>|<\/html>|<body[^>]*>|<\/body>/', '', $modified_html ) );
}
add_filter( 'render_block_core/calendar', 'fix_polylang_calendar_dom_patch_v2', 20, 2 );

Summary

When dealing with problems left over from history in WordPress, you need to weigh the advantages and disadvantages of ‘complete rewrite’ and ‘post patching’. This repair has successfully solved the problem of multilingual compatibility by retaining the original calendar logic and only modifying the data and links, and avoided the introduction of new bugs. For similar problems, it is recommended to prioritize the modification scheme based on the native structure to ensure stability and compatibility.

Is the search result page too long? I did a “break away” for the WordPress 2025 theme WordPress Theme Migration: Does Emoji Process Code Need to Keep?

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.