从Ad Inserter到WPCode:CTA配置迁移与优先级实现

再次无效后,我决定使用Auto Insert,Location选择"Frontend Only",并在代码中确定插入位置(图5)。

前言

在网站维护过程中,我遇到了一个重要挑战:将CTA(行动号召)配置从Ad Inserter迁移到WPCode,并实现更灵活的优先级判断。本文将详细介绍这个迁移过程和实现方法。

问题发现

在将网站从Hueman主题迁移到WordPress 2025主题后,我发现某些系列下的文章不再显示相应的CTA描述(图1)。这让我意识到需要重新配置CTA显示机制。

在将网站从Hueman主题迁移到WordPress 2025主题后,我发现某些系列下的文章不再显示相应的CTA描述(图1)


问题分析

经过分析,我发现Ad Inserter在新主题中存在兼容性问题。因此,我决定将CTA配置迁移到WPCode,且可以实现更灵活的优先级判断(图2)。

经过分析,我发现Ad Inserter在新主题中存在兼容性问题。因此,我决定将CTA配置迁移到WPCode,且可以实现更灵活的优先级判断(图2)。

解决方案探索:系列类别方案

最初,我考虑过基于系列类别来实现CTA配置。这个方案的优势在于:

  1. 层级关系清晰:系列类别可以更好地组织和管理多个系列
  2. 扩展性强:可以轻松添加新的系列到现有类别中
  3. 逻辑直观:通过父级关系可以更好地理解系列之间的关联
    然而,在实现过程中,我发现这个方案存在一些挑战:
  • 系列类别关系复杂:需要确保每个系列正确分配到相应的类别
  • 调试困难:在WordPress中,系列类别的层级关系有时不够直观
  • 时间限制:由于项目时间紧迫,无法完全实现和测试这个方案
    最终,由于时间关系,我决定暂时放弃基于系列类别的方案,转而采用更直接的基于系列slug的优先级实现。

最终方案:迁移到WPCode与优先级实现

步骤1:在主题编辑器中添加Shortcode

首先,我在主题编辑器中,单篇文章 – 模板,内容区块后添加简码区块,然后粘贴 wpcode

(图3)。

图3 在主题编辑器中,单篇文章 - 模板,内容区块后添加简码区块,然后粘贴
图3 在主题编辑器中,单篇文章 – 模板,内容区块后添加简码区块,然后粘贴

步骤2:尝试Auto Insert

然而,这种方法无效。于是,我尝试使用Auto Insert,Location选择”Insert After Content”(图4)。

然而,这种方法无效。于是,我尝试使用Auto Insert,Location选择"Insert After Content"(图4)。

步骤3:调整Auto Insert设置

再次无效后,我决定使用Auto Insert,Location选择”Frontend Only”,并在代码中确定插入位置(图5)。

再次无效后,我决定使用Auto Insert,Location选择"Frontend Only",并在代码中确定插入位置(图5)。

步骤4:成功实现

最终,这种方法成功了!一个系列下的文章会基于这个系列下的描述显示出来(图6)。

最终,这种方法成功了!一个系列下的文章会基于这个系列下的描述显示出来(图6)。

代码实现

以下是完整的代码实现,包含了优先级判断逻辑:

PHP
// ==========================================
// 使用 WordPress 原生钩子,强制在内容后插入
// ==========================================
add_filter('the_content', 'custom_desc_and_ads_inserter', 20);

function custom_desc_and_ads_inserter($content) {
    // 1. 仅在单篇文章页面执行,排除后台、Feed、首页等
    if (!is_singular('post') || is_admin() || is_feed()) {
        return $content;
    }

    // 2. 安全获取文章ID
    $post_id = get_the_ID();
    if (!$post_id) {
        global $wp_query;
        if (isset($wp_query->queried_object_id)) {
            $post_id = $wp_query->queried_object_id;
        }
    }

    // 3. 初始化变量
    $current_lang = '';
    $current_series_slugs = array();

    if (class_exists('SitePress')) {
        $current_lang = apply_filters('wpml_current_language', null);
    }
    if (empty($current_lang)) {
        $current_lang = (false !== strpos($_SERVER['REQUEST_URI'], '/en/')) ? 'en' : 'zh';
    }

    if ($post_id) {
        $current_series = get_the_terms($post_id, 'series');
        if (is_array($current_series) && !is_wp_error($current_series)) {
            $current_series_slugs = wp_list_pluck($current_series, 'slug');
        }
    }

    $output_html = '';

    // ==========================================
    // 4. 优先级判断逻辑 (保持不变)
    // ==========================================
    if ($current_lang === 'zh' && in_array('self-hosted-vpn', $current_series_slugs)) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>拒绝折腾 | 专属 WireGuard VPN 代搭建服务</strong></p>
    <p>本频道长期实测各类网络优化方案。<strong>自用线路已连续稳定运行超过 1 个月,全程无掉线记录。</strong>如果你不想反复踩坑、折腾复杂的服务器与协议配置,欢迎联系我获取专属解决方案。</p>
    <p><strong>适合以下用户:</strong><br>
✅ ChatGPT、Claude、Gemini 等 AI 工具使用者<br>
✅ 海外远程办公用户<br>
✅ 技术学习与开发环境访问需求<br>
✅ 不想长期折腾 VPS 与代理配置的用户<br>
✅ 希望拥有专属节点而非公共机场的用户
    </p>
    <p><strong>服务内容:</strong><br>
<strong>远程代搭建</strong>:在你自己的服务器上部署专属 VPN,数据完全掌控。<br>
<strong>免费试用</strong>:可申请免费试用一个月我的自建节点。<br>
<strong>分流优化</strong>:针对 AI 工具、开发环境及日常使用进行深度优化。<br>
<strong>后续支持</strong>:部署完成后可协助排查常见问题。
    </p>
    <p>如需了解方案或申请试用,请直接联系我,并注明:<strong>VPN 咨询</strong></p>
    <p><strong>联系方式:</strong><br>
Telegram:@shuijingwan<br>
微信:13980074657<br>
邮箱:<a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'en' && in_array('self-hosted-vpn-en', $current_series_slugs)) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>Stop the Hassle | Dedicated WireGuard VPN Deployment Service</strong></p>
    <p>I continuously test and evaluate various network optimization solutions. <strong>My personal setup has been running stably for over a month with zero downtime.</strong> If you are tired of troubleshooting, experimenting with different protocols, or managing complex server configurations, feel free to contact me for a dedicated solution.</p>
    <p><strong>Ideal For:</strong><br>
✅ Users of AI tools such as ChatGPT, Claude, and Gemini<br>
✅ Remote workers who rely on stable international connectivity<br>
✅ Developers and technical learners who need access to global resources<br>
✅ Users who do not want to spend time managing VPS and proxy configurations<br>
✅ Anyone who prefers a private VPN server instead of shared public services
    </p>
    <p><strong>What I Offer:</strong><br>
<strong>Remote Deployment</strong>: Deploy a dedicated WireGuard VPN on your own server with full control over your data.<br>
<strong>Free Trial</strong>: Apply for a one-month free trial of my self-hosted VPN node before making a decision.<br>
<strong>Traffic Routing Optimization</strong>: Fine-tuned routing rules optimized for AI tools, development environments, and everyday browsing.<br>
<strong>Post-Deployment Support</strong>: Assistance with setup, client configuration, and troubleshooting after deployment.
    </p>
    <p>If you would like to learn more or apply for a free trial, please contact me directly and mention: <strong>VPN Consultation</strong>.</p>
    <p><strong>Contact Me:</strong><br>
Telegram: @shuijingwan<br>
WeChat: 13980074657<br>
Email: <a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'zh' && array_intersect($current_series_slugs, array('wp-perf-notes', 'wp-blog-multilingual-guide-zh', 'blog-seo-log', 'exploring-blog-business-models', 'ad-inserter-usage-notes', 'from-classical-to-block-theme-migration'))) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>技术博客运营与商业化咨询服务</strong></p>
    <p>本站已持续运营超过 10 年,累计发布 1000+ 篇原创技术文章,长期实践 WordPress 网站建设、性能优化、多语言运营、Google SEO 以及广告变现等方向。<strong>所有经验均来自真实网站运营过程中的持续实践与验证。</strong>如果你正在运营个人博客、技术网站或独立开发者项目,希望获得更多自然流量并探索网站商业化路径,欢迎联系我交流。</p>
    <p><strong>适合以下用户:</strong><br>
✅ 技术博客运营者<br>
✅ 独立开发者与个人站长<br>
✅ SaaS 产品官网运营团队<br>
✅ 希望拓展海外流量的网站运营者<br>
✅ 希望通过网站获得收入的内容创作者
    </p>
    <p><strong>服务内容:</strong><br>
<strong>WordPress 性能优化</strong><br>
<strong>SEO 优化咨询</strong><br>
<strong>多语言网站建设</strong><br>
<strong>广告收益优化</strong><br>
<strong>博客运营与商业化咨询</strong>
    </p>
    <p>如需了解方案或交流相关问题,请直接联系我,并注明:<strong>博客运营咨询</strong></p>
    <p><strong>联系方式:</strong><br>
Telegram:@shuijingwan<br>
微信:13980074657<br>
邮箱:<a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'en' && array_intersect($current_series_slugs, array('wp-perf-notes-en', 'wp-blog-multilingual-guide', 'blog-seo-log-en', 'exploring-blog-business-models-en', 'ad-inserter-usage-notes-en', 'from-classical-to-block-theme-migration-en'))) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>Technical Blog Growth & Monetization Consulting</strong></p>
    <p>I have been running my personal technology blog for more than 10 years and have published over 1,000 original articles covering WordPress optimization, multilingual websites, Google SEO, content strategy, and website monetization. <strong>All insights shared on this site come from real-world operation and long-term experimentation.</strong> If you are building a blog, developer website, SaaS project, or content-driven platform, I would be happy to share practical experience and optimization suggestions.</p>
    <p><strong>Ideal For:</strong><br>
✅ Technical bloggers<br>
✅ Independent developers<br>
✅ SaaS website owners<br>
✅ Content creators seeking organic traffic growth<br>
✅ Website owners interested in monetization opportunities
    </p>
    <p><strong>What I Offer:</strong><br>
✅ WordPress Performance Optimization<br>
✅ SEO Consulting<br>
✅ Multilingual Website Setup<br>
✅ Ad Revenue Optimization<br>
✅ Blog Growth & Monetization Consulting
    </p>
    <p>If you would like to discuss your website, traffic growth strategy, or monetization opportunities, please contact me and mention: <strong>Blog Growth Consultation</strong>.</p>
    <p><strong>Contact Me:</strong><br>
Telegram: @shuijingwan<br>
WeChat: 13980074657<br>
Email: <a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'zh' && in_array('yii2-advanced-api-rpc-i18n-logging', $current_series_slugs)) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>PHP / Yii2 企业系统开发与架构咨询服务</strong></p>
    <p>拥有 15 年以上 Web 开发经验,长期从事企业管理系统、数据平台、API 服务及高并发业务系统开发。曾主导多个大型项目的架构设计与核心模块研发,具备丰富的 Yii2 企业级项目实战经验。</p>
    <p><strong>适合以下项目:</strong><br>
✅ 企业管理系统<br>
✅ ERP / CRM 系统<br>
✅ RESTful API 平台<br>
✅ 多租户 SaaS 系统<br>
✅ 老旧 PHP 系统升级改造
    </p>
    <p><strong>服务内容:</strong><br>
✅ Yii2 企业项目开发<br>
✅ RESTful API 设计与开发<br>
✅ 系统架构设计<br>
✅ 性能优化与故障排查<br>
✅ 长期技术支持与维护
    </p>
    <p>如需咨询,请联系我,并注明:<strong>Yii2 项目咨询</strong></p>
    <p><strong>联系方式:</strong><br>
Telegram:@shuijingwan<br>
微信:13980074657<br>
邮箱:<a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'en' && in_array('yii2-advanced-api-rpc-i18n-logging-en', $current_series_slugs)) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>PHP / Yii2 Enterprise Development & Architecture Consulting</strong></p>
    <p>With more than 15 years of web development experience, I have worked extensively on enterprise platforms, business management systems, API services, and large-scale web applications. I have led architecture design and core system development for multiple production projects using Yii2.</p>
    <p><strong>Ideal For:</strong><br>
✅ Enterprise applications<br>
✅ ERP / CRM systems<br>
✅ RESTful API platforms<br>
✅ Multi-tenant SaaS projects<br>
✅ Legacy PHP modernization
    </p>
    <p><strong>What I Offer:</strong><br>
✅ Yii2 Enterprise Development<br>
✅ API Architecture Design<br>
✅ System Architecture Consulting<br>
✅ Performance Optimization<br>
✅ Long-Term Technical Support
    </p>
    <p>Please contact me and mention: <strong>Yii2 Project Consultation</strong>.</p>
    <p><strong>Contact Me:</strong><br>
Telegram: @shuijingwan<br>
WeChat: 13980074657<br>
Email: <a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'zh' && in_array('practical-go-concurrency-step-by-step', $current_series_slugs)) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>Go 后端开发与架构咨询服务</strong></p>
    <p>长期从事 Go 语言后端开发与高并发系统建设,具备 API 服务、实时通信、任务调度、微服务架构及系统优化经验。相关实践内容均来自真实生产环境项目。</p>
    <p><strong>适合以下项目:</strong><br>
✅ 高并发业务系统<br>
✅ API 服务平台<br>
✅ WebSocket 实时通信<br>
✅ 微服务架构建设<br>
✅ 性能优化与系统重构
    </p>
    <p><strong>服务内容:</strong><br>
✅ Go 后端开发<br>
✅ API 服务开发<br>
✅ 微服务架构设计<br>
✅ 系统性能优化<br>
✅ 技术咨询与长期支持
    </p>
    <p>如需咨询,请联系我,并注明:<strong>Go 项目咨询</strong></p>
    <p><strong>联系方式:</strong><br>
Telegram:@shuijingwan<br>
微信:13980074657<br>
邮箱:<a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'en' && in_array('practical-go-concurrency-step-by-step-en', $current_series_slugs)) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>Go Backend Development & Architecture Consulting</strong></p>
    <p>I specialize in Go backend development, high-concurrency systems, API platforms, real-time communication services, and scalable backend architectures. All content shared in this series is based on real-world production experience.</p>
    <p><strong>Ideal For:</strong><br>
✅ High-concurrency applications<br>
✅ API service platforms<br>
✅ Real-time communication systems<br>
✅ Microservices architecture<br>
✅ Backend modernization projects
    </p>
    <p><strong>What I Offer:</strong><br>
✅ Go Backend Development<br>
✅ API Platform Development<br>
✅ Microservices Architecture Design<br>
✅ Performance Optimization<br>
✅ Long-Term Technical Support
    </p>
    <p>Please contact me and mention: <strong>Go Project Consultation</strong>.</p>
    <p><strong>Contact Me:</strong><br>
Telegram: @shuijingwan<br>
WeChat: 13980074657<br>
Email: <a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'zh' && in_array('windows-to-ubuntu', $current_series_slugs)) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>Linux 服务器部署与运维咨询服务</strong></p>
    <p>长期使用 Ubuntu、Debian 等 Linux 系统进行开发与运维工作,积累了服务器部署、Docker 容器化、Nginx 配置、网站迁移及性能优化等实践经验。</p>
    <p><strong>适合以下场景:</strong><br>
✅ Linux 环境迁移<br>
✅ 网站部署上线<br>
✅ Docker 容器化部署<br>
✅ Nginx 配置优化<br>
✅ 服务器性能调优
    </p>
    <p><strong>服务内容:</strong><br>
✅ Linux 环境搭建<br>
✅ 网站部署与迁移<br>
✅ Docker 配置与维护<br>
✅ Nginx 优化<br>
✅ 长期运维支持
    </p>
    <p>如需咨询,请联系我,并注明:<strong>Linux 运维咨询</strong></p>
    <p><strong>联系方式:</strong><br>
Telegram:@shuijingwan<br>
微信:13980074657<br>
邮箱:<a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'en' && in_array('windows-to-ubuntu-migration-configuration-guide', $current_series_slugs)) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>Linux Server Deployment & Operations Consulting</strong></p>
    <p>I have extensive experience working with Ubuntu, Debian, Docker, Nginx, and cloud servers in production environments. This includes server deployment, website migration, performance tuning, and long-term maintenance.</p>
    <p><strong>Ideal For:</strong><br>
✅ Linux migration projects<br>
✅ Website deployment<br>
✅ Docker-based environments<br>
✅ Nginx optimization<br>
✅ Server performance tuning
    </p>
    <p><strong>What I Offer:</strong><br>
✅ Linux Environment Setup<br>
✅ Website Migration & Deployment<br>
✅ Docker Configuration & Maintenance<br>
✅ Nginx Optimization<br>
✅ Long-Term Technical Support
    </p>
    <p>Please contact me and mention: <strong>Linux Operations Consultation</strong>.</p>
    <p><strong>Contact Me:</strong><br>
Telegram: @shuijingwan<br>
WeChat: 13980074657<br>
Email: <a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'zh' && (custom_in_category_tree('laravel') || custom_in_category_tree('yii-2'))) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>PHP / Yii2 / Laravel 企业系统开发与架构咨询服务</strong></p>
    <p>拥有 15 年以上 Web 开发经验,长期从事企业管理系统、数据平台、API 服务及高并发业务系统开发。熟悉 PHP、Yii2、Laravel、MySQL、Redis 等技术栈,曾主导多个大型项目的架构设计与核心模块研发,具备丰富的企业级项目实战经验。</p>
    <p><strong>适合以下项目:</strong><br>
✅ 企业管理系统<br>
✅ ERP / CRM 系统<br>
✅ RESTful API 平台<br>
✅ 多租户 SaaS 系统<br>
✅ 老旧 PHP 系统升级改造
    </p>
    <p><strong>服务内容:</strong><br>
✅ PHP / Yii2 / Laravel 项目开发<br>
✅ RESTful API 设计与开发<br>
✅ 系统架构设计<br>
✅ 性能优化与故障排查<br>
✅ 长期技术支持与维护
    </p>
    <p>如需咨询,请联系我,并注明:<strong>PHP 项目咨询</strong></p>
    <p><strong>联系方式:</strong><br>
Telegram:@shuijingwan<br>
微信:13980074657<br>
邮箱:<a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'en' && (custom_in_category_tree('laravel-en') || custom_in_category_tree('yii-2-en'))) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>PHP / Yii2 / Laravel Enterprise Development & Architecture Consulting</strong></p>
    <p>With more than 15 years of web development experience, I have worked extensively on enterprise platforms, business management systems, API services, and large-scale web applications. My expertise covers PHP, Yii2, Laravel, MySQL, Redis, RESTful APIs, and scalable backend architectures. I have led architecture design and core system development for multiple production projects and long-term enterprise systems.</p>
    <p><strong>Ideal For:</strong><br>
✅ Enterprise applications<br>
✅ ERP / CRM systems<br>
✅ RESTful API platforms<br>
✅ Multi-tenant SaaS projects<br>
✅ Legacy PHP system modernization
    </p>
    <p><strong>What I Offer:</strong><br>
✅ PHP / Yii2 / Laravel Development<br>
✅ RESTful API Design & Development<br>
✅ System Architecture Consulting<br>
✅ Performance Optimization & Troubleshooting<br>
✅ Long-Term Technical Support & Maintenance
    </p>
    <p>If you would like to discuss your project, please contact me and mention: <strong>PHP Project Consultation</strong>.</p>
    <p><strong>Contact Me:</strong><br>
Telegram: @shuijingwan<br>
WeChat: 13980074657<br>
Email: <a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'zh' && (custom_in_category_tree('linux') || custom_in_category_tree('cloud-vendor'))) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>Linux 服务器部署与运维咨询服务</strong></p>
    <p>长期使用 Ubuntu、Debian 等 Linux 系统进行开发与运维工作,积累了服务器部署、Docker 容器化、Nginx 配置、网站迁移及性能优化等实践经验。</p>
    <p><strong>适合以下场景:</strong><br>
✅ Linux 环境迁移<br>
✅ 网站部署上线<br>
✅ Docker 容器化部署<br>
✅ Nginx 配置优化<br>
✅ 服务器性能调优
    </p>
    <p><strong>服务内容:</strong><br>
✅ Linux 环境搭建<br>
✅ 网站部署与迁移<br>
✅ Docker 配置与维护<br>
✅ Nginx 优化<br>
✅ 长期运维支持
    </p>
    <p>如需咨询,请联系我,并注明:<strong>Linux 运维咨询</strong></p>
    <p><strong>联系方式:</strong><br>
Telegram:@shuijingwan<br>
微信:13980074657<br>
邮箱:<a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } elseif ($current_lang === 'en' && (custom_in_category_tree('linux-en') || custom_in_category_tree('cloud-vendor-en'))) {
        $output_html .= <<<HTML
<div class="custom-desc-block">
    <p><strong>Linux Server Deployment & Operations Consulting</strong></p>
    <p>I have extensive experience working with Ubuntu, Debian, Docker, Nginx, and cloud servers in production environments. This includes server deployment, website migration, performance tuning, and long-term maintenance.</p>
    <p><strong>Ideal For:</strong><br>
✅ Linux migration projects<br>
✅ Website deployment<br>
✅ Docker-based environments<br>
✅ Nginx optimization<br>
✅ Server performance tuning
    </p>
    <p><strong>What I Offer:</strong><br>
✅ Linux Environment Setup<br>
✅ Website Migration & Deployment<br>
✅ Docker Configuration & Maintenance<br>
✅ Nginx Optimization<br>
✅ Long-Term Technical Support
    </p>
    <p>Please contact me and mention: <strong>Linux Operations Consultation</strong>.</p>
    <p><strong>Contact Me:</strong><br>
Telegram: @shuijingwan<br>
WeChat: 13980074657<br>
Email: <a href="mailto:shuijingwanwq@gmail.com">shuijingwanwq@gmail.com</a></p>
</div>
HTML;
    } else {
        if ($current_lang === 'zh') {
            $output_html .= <<<HTML
<div class="custom-desc-block">
    <p>我是拥有 15+ 年经验的 PHP / Go 后端工程师。如需以下服务,欢迎联系我(更多介绍请查看 <a href="https://www.shuijingwanwq.com/about-me-contact/" target="_blank" rel="noopener noreferrer">关于我 & 合作</a>):</p>
    <ul>
        <li>✅ PHP / Go 项目开发与维护</li>
        <li>✅ 系统架构设计与技术咨询</li>
        <li>✅ 网站性能优化与故障排查</li>
        <li>✅ Linux 服务器部署与运维</li>
        <li>✅ 网络环境优化与远程支持</li>
        <li>✅ 长期技术顾问合作</li>
    </ul>
    <p>
        微信:13980074657<br>
        邮箱:shuijingwanwq@gmail.com<br>
        Telegram:@shuijingwan<br>
        GitHub:https://github.com/shuijingwan
    </p>
</div>
HTML;
        } else {
            $output_html .= <<<HTML
<div class="custom-desc-block">
    <p>I am a PHP / Go backend engineer with 15+ years of experience. If you need any of the following services, feel free to contact me (more details: <a href="https://www.shuijingwanwq.com/en/about-me-contact-2/" target="_blank" rel="noopener noreferrer">About Me & Collaboration</a>):</p>
    <ul>
        <li>✅ PHP / Go project development and maintenance</li>
        <li>✅ System architecture design and technical consulting</li>
        <li>✅ Performance optimization and troubleshooting</li>
        <li>✅ Linux server deployment and operations</li>
        <li>✅ Network environment optimization and remote support</li>
        <li>✅ Long-term technical consulting</li>
    </ul>
    <p>
        WeChat: 13980074657<br>
        Email: shuijingwanwq@gmail.com<br>
        Telegram: @shuijingwan<br>
        GitHub: https://github.com/shuijingwan
    </p>
</div>
HTML;
        }
    }

    // 5. 将描述追加到文章内容末尾
    return $content . $output_html;
}

// ==========================================
// 辅助函数:检查分类树
// ==========================================
function custom_in_category_tree($cat_slug) {
    global $post;
    if (!is_object($post) || !isset($post->ID)) return false;
    
    $cat = get_category_by_slug($cat_slug);
    if (!$cat) return false;
    $term_ids = get_term_children($cat->term_id, 'category');
    $term_ids[] = $cat->term_id;
    return in_category($term_ids, $post->ID);
}

优先级实现:分类树功能测试

我还测试了 demo1_custom_in_category_tree('linux') 功能。发现某一篇文章,不属于分类Linux,只属于其下的孙分类Ubuntu 26.04(图7)。

我还测试了 custom_in_category_tree('linux') 功能。发现某一篇文章,不属于分类Linux,只属于其下的孙分类Ubuntu 26.04(图7)。

优先级验证:预期结果

即使文章只属于孙分类,仍然会显示 custom_in_category_tree('linux') 中的描述,这符合我们的优先级预期(图8)。

令人惊喜的是,即使文章只属于孙分类,仍然会显示 custom_in_category_tree('linux') 中的描述,这符合我们的优先级预期(图8)。

结论

通过这次迁移,我成功地将CTA配置从Ad Inserter迁移到WPCode,并实现了更灵活的优先级判断。虽然最初考虑过基于系列类别的方案,但由于时间关系,最终选择了基于系列slug的优先级实现。新方案不仅解决了主题切换后的系列描述显示问题,还提供了更强大的分类树功能。
希望这篇文章对正在考虑类似迁移的开发者有所帮助!

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理