admin管理员组文章数量:1122832
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/en/products
But for the swedish I want
www.mypage/sv/produkter
And other language :
www.mypage/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.
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.
Share Improve this question edited Jul 16, 2020 at 17:52 mozboz 2,6081 gold badge12 silver badges23 bronze badges asked Jul 16, 2020 at 12:39 AndreeezAndreeez 111 silver badge3 bronze badges2 Answers
Reset to default 0So 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/
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' );
本文标签: url rewritingTranslate custom post type and taxonomy slug in URL
版权声明:本文标题:url rewriting - Translate custom post type and taxonomy slug in URL? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290967a1928624.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论