草庐IT

php - 无法在 WPML 中翻译一个自定义帖子类型 slug

coder 2024-01-01 原文

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

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

英文网址为http://brigade-electronics.com/nl/products/backeye360/

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

相反,我在启用翻译 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 错误。如果我将这些保留为英文,则产品部分可以正常工作。

谢谢

最佳答案

试试这个

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

关于php - 无法在 WPML 中翻译一个自定义帖子类型 slug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44410257/

有关php - 无法在 WPML 中翻译一个自定义帖子类型 slug的更多相关文章

  1. ruby - i18n Assets 管理/翻译 UI - 2

    我正在使用i18n从头开始​​构建一个多语言网络应用程序,虽然我自己可以处理一大堆yml文件,但我说的语言(非常)有限,最终我想寻求外部帮助帮助。我想知道这里是否有人在使用UI插件/gem(与django上的django-rosetta不同)来处理多个翻译器,其中一些翻译器不愿意或无法处理存储库中的100多个文件,处理语言数据。谢谢&问候,安德拉斯(如果您已经在ruby​​onrails-talk上遇到了这个问题,我们深表歉意) 最佳答案 有一个rails3branchofthetolkgem在github上。您可以通过在Gemfi

  2. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  5. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  6. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  7. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  10. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

随机推荐