【问题标题】:Unable to translate one custom post type slug in WPML无法在 WPML 中翻译一种自定义帖子类型 slug
【发布时间】: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() 回显字符串,在这种情况下不起作用

标签: php wordpress wpml


【解决方案1】:

试试这个

    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 );
    }

【讨论】:

  • 恐怕这没有什么区别,在切换到翻译的 slug 时仍然会出现 404 错误。
  • 您是否尝试过禁用和重新启用“Pretty Permalinks”?
  • 是的,仍然没有区别。
  • @terrorfall 接受这个答案,这样它就可以帮助遇到类似问题的其他用户。
【解决方案2】:

你刷新了重写规则吗?

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

注意:如果在插件中注册帖子类型,请调用 激活和停用挂钩中的 flush_rewrite_rules()(请参阅 在下面的激活时刷新重写)。如果 flush_rewrite_rules() 不是 使用,那么你将不得不手动进入设置>永久链接和 在您的自定义帖子类型之前刷新您的永久链接结构 显示正确的结构。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-21
    • 2016-12-04
    • 2013-09-10
    • 2012-06-30
    • 2021-10-17
    • 2018-11-23
    • 2017-01-31
    • 2014-03-05
    相关资源
    最近更新 更多