我现在正在研究一个类似的解决方案-该网站必须在所有URL中都有语言代码(默认语言除外),但只有页面可以通过WPML/Polylang插件的方式进行翻译。对于新闻(博客),我们只显示特定语言的帖子(它们是独立的,不是彼此的翻译)。所有其他内容以所有语言混合。此外,UI也以URL中的语言集显示。
下面是我为用语言代码预先编写重写规则所做的工作:
function prepend_default_rewrite_rules( $rules ) {
// Prepare for new rules
$new_rules = [];
// Set up languages, except default one
$language_slugs = [\'ar\', \'ku\'];
// Generate language slug regex
$languages_slug = \'(?:\' . implode( \'/|\', $language_slugs ) . \'/)?\';
// Set up the list of rules that don\'t need to be prefixed
$whitelist = [
\'^wp-json/?$\',
\'^wp-json/(.*)?\',
\'^index.php/wp-json/?$\',
\'^index.php/wp-json/(.*)?\'
];
// Set up the new rule for home page
$new_rules[\'(?:\' . implode( \'/|\', $language_slugs ) . \')/?$\'] = \'index.php\';
// Loop through old rules and modify them
foreach ( $rules as $key => $rule ) {
// Re-add those whitelisted rules without modification
if ( in_array( $key, $whitelist ) ) {
$new_rules[ $key ] = $rule;
// Update rules starting with ^ symbol
} elseif ( substr( $key, 0, 1 ) === \'^\' ) {
$new_rules[ $languages_slug . substr( $key, 1 ) ] = $rule;
// Update other rules
} else {
$new_rules[ $languages_slug . $key ] = $rule;
}
}
// Return out new rules
return $new_rules;
}
add_filter( \'rewrite_rules_array\', \'prepend_default_rewrite_rules\' );
还没有对其进行全面测试-只是在研究了WP\\U重写类后提出了解决方案。所以这是一项正在进行的工作。希望这能有所帮助。
P、 S:另外,我很高兴看到您的解决方案“我从URL检索值以确定语言/国家=完整”-这就是我目前正在研究的:)