首页 > 解决方案 > 如何更改自定义帖子的语言?

问题描述

polylang在我的项目中使用多种语言的插件。我使用以下代码创建了一个自定义事件。

function post_types()
{
    $labels = array(
        "name"          => "Events",
        "add_new_items" => "Add New Event",
        "edit_item"     => "Edit Event",
        "all_items"     => "All Events",
        "singular_name" => "Event"
    );

    $events = array(
        "has_archive" => true,
        "public"      => true,
        "menu_icon"   => "dashicons-calendar",
        "labels"      => $labels,
    );
    register_post_type("event", $events);
}

add_action("init", "post_types");

我不确定如何更改此活动中帖子的语言?

标签: phpwordpresspolylang

解决方案


请使用文本域:

function post_types()
{
    $labels = array(
        "name"          => __( "Events", "your-text-domain" ),
        "add_new_items" => __( "Add New Event", "your-text-domain" ),
        "edit_item"     => __( "Edit Event", "your-text-domain" ),
        "all_items"     => __( "All Events", "your-text-domain" ),
        "singular_name" => __( "Event, "your-text-domain" )"
    );

    $events = array(
        "has_archive" => true,
        "public"      => true,
        "menu_icon"   => "dashicons-calendar",
        "labels"      => $labels,
    );
    register_post_type("event", $events);
}

add_action("init", "post_types");

然后为您想要的语言创建一个语言文件 (.po/.mo)。我基本上使用 Loco Translate Plugin 来创建语言文件。

了解更多:https ://developer.wordpress.org/apis/handbook/internationalization/localization/


推荐阅读