【发布时间】:2017-11-08 15:58:39
【问题描述】:
我已经在 WPML 论坛上提出了这个问题,但希望这里的人能够提供帮助。
我正在尝试将 slug 翻译为自定义帖子类型
英文网址是http://brigade-electronics.com/nl/products/backeye360/
翻译后的网址应该是http://brigade-electronics.com/nl/producten/backeye360/
相反,在启用 translate slug 选项后导航到 URL 时出现 404 错误
复制问题的步骤:
- 点击 WPML -> 翻译选项
- 启用翻译自定义帖子 slug(通过 WPML 字符串翻译)。
- 在自定义帖子设置下(在同一页面上)点击翻译复选框
- 为每种语言添加了翻译的 slug
- 点击保存
- 导航到前端,仅在产品部分看到 404 错误。
我已运行故障排除页面中的所有选项,以清理数据库。
这似乎只适用于产品部分中的某些页面。最奇怪的部分是该网站的加拿大部分,因为“产品”一词是英文的,因此无论是否有翻译的 slug,URL 都保持不变,但是,我仍然在这些页面上收到 404 错误。
还值得注意的是,所有其他自定义帖子类型都可以正常工作。
自定义帖子类型已以标准方式注册
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 的新翻译出现在“字符串翻译”部分,更新这些字符串时,我得到相同的 404 错误。如果我将这些保留为英文,则产品部分可以正常工作。
谢谢
【问题讨论】:
-
翻译 slug 在哪里 $labels = array( 'name' => __( 'Products', '' ), 'singular_name' => __( 'Product', '') ); - 文本域丢失
-
这对所有其他自定义帖子类型没有影响,它们的设置都相同,但会尝试一下并让您知道
-
嗨@MujeebuRahman 添加域没有任何作用
-
你试过 _e() 吗?
-
@MujeebuRahman __e() 回显字符串,在这种情况下不起作用