TransWikia.com

Translate custom post type and taxonomy slug in URL?

WordPress Development Asked by Andreeez on October 30, 2021

I need help how to translate the custom post type and taxonomy when using multisite and multi-language.
I am using subdirectory a /en /sv etc.

Are using the plugin (Multisite Language Switcher), but can not change the rewrite settings there. So I am guesing I have to change some rewrite?
Or should I translate the post type with translations file, .mo .po?

This is how the post type set up are in functions.php.
Should I do something with the rewrite?

function create_posttype_product() {
    register_post_type( 'product',
        array(
            'labels' => array(
                'name' => __('Products'),
                'singular_name' => __('product'),
                'add_new' => __('Add new product'),
                'add_new_item' => __('New product'),
                'edit_item' => __('Edit product')
            ),
            'public' => true,
            'rewrite' => array( 'slug' => 'product', 'with_front' => false ),

            'has_archive' => 'product',
            'menu_icon'   => 'dashicons-editor-help',
            'supports' => array('title', 'editor', 'thumbnail')
        )
    );
}
add_action( 'init', 'create_posttype_product' );

So for example on english webpage url would be:
www.mypage.com/en/products

But for the swedish I want
www.mypage.com/sv/produkter

And other language :
www.mypage.com/xx/product-name-in-this-language

How can I manage to get this result? I have searched and search and can not find the right answer.

2 Answers

To use the rewrite option you have to put "product" in variable. Warning : you have to let the same value "product" for "register_post_type".

function create_posttype_product() {

 if  ($language == 'sv') {
      $productSlug = 'produkter';   
 } else {
      $productSlug = 'product';   
 }

register_post_type( 'product',
    array(
        'labels' => array(
            'name' => __('Products'),
            'singular_name' => __('product'),
            'add_new' => __('Add new product'),
            'add_new_item' => __('New product'),
            'edit_item' => __('Edit product')
        ),
        'public' => true,
        'rewrite' => array( 'slug' => $productSlug, 'with_front' => false ),

        'has_archive' => 'product',
        'menu_icon'   => 'dashicons-editor-help',
        'supports' => array('title', 'editor', 'thumbnail')
    )
)}; 
add_action( 'init', 'create_posttype_product' );

Answered by Thomas FromWood on October 30, 2021

So firstly whilst this may be possible I imagine that it is probably not best practice. This is because semantically, and for SEO it makes more sense to have fewer URLs.

It could be understood that if your page is /en/foo and I want to switch to French, I can just go to /fr/foo. People may do this manually, and some automated tools may understand the site works like this. If a person or a tool also has to translate 'foo' to French to find the correct page, this is more complicaed and it means everyone has to agree on translations as well as language codes, and translations may not always be 100% the same.

I think this is why it's hard to find solutions to this in searching - generally it is not done so that URLs stay as similar as possible and only the language part changes.

If you do want to do it and you don't have many languages, one way to do this would be to find the manual translations and add .htaccess rules to invisbly rewrite the URL with rules in your root .htaccess something like:

RewriteRule ^/sv/produkter(/.*) /en/product$1 [L]

This would be easy to do as long as you didn't have many slugs or languages to do it for.

There's also some possibility the PolyLang plugin might do this but you'd need to look into that further. See point 3 here: https://polylang.pro/doc/url-modifications/

Answered by mozboz on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP