You need to enable JavaScript to run this app. PHP结合WordPress实现国家全称转两位ISO国家代码的方法
优惠活动
大模型
产品
解决方案
定价
更多

PHP结合WordPress实现国家全称转两位ISO国家代码的方法

岗位列表模块国家名称转ISO代码开发需求
  • 适用开发场景:面向招聘岗位列表(WordPress jobs 自定义文章类型)的功能开发,系统当前已在location自定义分类法下存储对应国家的完整名称,核心目标是实现国家全称到ISO 3166-1标准两位国家代码的转换,典型转换规则为United States 映射为 US、Netherlands 映射为 NL
  • 已编写的核心业务代码:
$locations = array();

$args = array (
    'taxonomy' => 'location',
    'hide_empty' => true,
    'orderby' => 'name',
    'order' => 'ASC',
);

$get_locations = get_terms($args);

foreach ($get_locations as $key => $location) {
    $jobs_array = array();
    $location_name = $location->name;
    
    // 查询当前location分类下的已发布招聘岗位
    $post_type = 'jobs';
    $posts_per_page = -1;
    $jobs_args = array(
        'post_type' => $post_type,
        'post_status' => 'publish',
        'posts_per_page' => $posts_per_page,
        'location' => $location_slug,
    );
    
    $jobs = new WP_Query( $jobs_args );
    
    if($jobs->have_posts()) :
        while ( $jobs->have_posts() ): $jobs->the_post();
        // 归集岗位标题到对应分类的数组集合
        $jobs_array[] = $post->post_title;
        endwhile; wp_reset_postdata();
    endif;

    $locations[$location_slug] = $jobs_array;
}

注:现有代码片段存在未补全的逻辑疏漏:未从$location分类对象中读取slug属性赋值给$location_slug变量,运行时会出现查询参数为空的问题。

  • 当前代码运行效果:预期生成关联数组,数组的键为国家分类标识,对应值为该分类下归集的招聘岗位标题集合,实际数组结构参考如下:
    array_result
  • 核心诉求:整套逻辑基于WordPress文章查询循环搭建,需要输出可直接落地的国家名称到ISO两位代码的映射实现方案,适配现有代码流程,无需大规模重构。

内容的提问来源于stack exchange,提问作者Loosie94

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.10 08:24:04