无法翻译 WPML 中的一种自定义 post 类型别名

Unable to translate one custom post type slug in WPML

我已经在 WPML 论坛上提出了这个问题,但希望这里有人能够提供帮助。

我正在尝试为自定义 post 类型翻译 slug

英文URL是http://brigade-electronics.com/nl/products/backeye360/

翻译后的URL应该是http://brigade-electronics.com/nl/producten/backeye360/

相反,我在启用翻译 slug 选项后导航到 URL 时收到 404 错误

重复问题的步骤:

我在故障排除页面中有 运行 所有选项,以清理数据库。

这似乎只适用于产品部分中的某些页面。最奇怪的部分是该站点的加拿大部分,因为术语 'product' 是英文的,因此 URLs 在有或没有翻译的 slugs 的情况下保持不变,但是,我仍然得到这些页面上出现 404 错误。

还值得注意的是,所有其他自定义 post 类型都可以正常工作。

自定义 post 类型已以标准方式注册

function register_products_post_type() {

    $labels = array(
        'name' => __( 'Products', '' ),
        'singular_name' => __( 'Product', '' )
    );

    $args = array(
        'label' => __( 'Products', '' ),
        'labels' => $labels,
        'description' => '',
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_rest' => false,
        'rest_base' => '',
        'has_archive' => false,
        'show_in_menu' => true,
        'exclude_from_search' => false,
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => true,
        'rewrite' => array( 'slug' => 'products', 'with_front' => false ),
        'query_var' => true,
        'menu_position' => 6,
        'menu_icon' => 'dashicons-cart',
        'supports' => array( 'title', 'thumbnail', 'page-attributes' )
    );

    register_post_type( 'products', $args );

}
add_action( 'init', 'register_products_post_type' );

根据下面的回答,上面的代码已经更新为

add_action( 'init', 'create_post_type');
function create_post_type() {
    $labels = array(
        'name'               => _x( 'Products', 'general name of the post type' ),
        'singular_name'      => _x( 'Products', 'name for one object of this post type' ),

    );
    $args = array(
        'labels' =>  $labels, // An array that defines the different labels assigned to the custom post type
        'public' =>  true, // To show the custom post type on the WordPress dashboard
        'supports' => array( 'title', 'thumbnail', 'page-attributes' ),
        'has_archive' =>  true, //Enables the custom post type archive at
        'hierarchical' =>  true, //Enables the custom post type to have a hierarchy
        'rewrite' => array( 'slug' =>  _x('products', 'URL slug')),
    );
    register_post_type( 'products', $args );
    }

slug 的新翻译出现在 'String Translation' 部分,更新这些字符串时,我得到相同的 404 错误。如果我将这些保留为英文,则产品部分可以正常工作。

谢谢

试试这个

    add_action( 'init', 'create_post_type');
    function create_post_type() {
        $labels = array(
        'name'               => _x( 'Products', 'general name of the post type' ),
        'singular_name'      => _x( 'Products', 'name for one object of this post type' ),

       );
       $args = array(
         'labels' =>  $labels, // An array that defines the different labels assigned to the custom post type
         'public' =>  true, // To show the custom post type on the WordPress dashboard
         'supports' => array( 'title', 'thumbnail', 'page-attributes' ),
         'has_archive' =>  true, //Enables the custom post type archive at 
         'hierarchical' =>  true, //Enables the custom post type to have a hierarchy
         'rewrite' => array( _x('slug' => 'products'), 'with_front' => false ),
    );
    register_post_type( 'products', $args );
    }

你刷新重写规则了吗?

转到“设置”>“永久链接”并刷新。

Note: If registering a post type inside of a plugin, call flush_rewrite_rules() in your activation and deactivation hook (see Flushing Rewrite on Activation below). If flush_rewrite_rules() is not used, then you will have to manually go to Settings > Permalinks and refresh your permalink structure before your custom post type will show the correct structure.

来源:https://codex.wordpress.org/Function_Reference/register_post_type