我正在尝试为我的网站创建自定义翻译。
Quick Story:
“我面对的是”;404在函数中添加自定义规则后,页面出现“找不到错误页面”问题。php
$newrules[\'^th/(.*)$\'] = \'index.php?name=$matches[1]\';
$newrules[\'^se/(.*)$\'] = \'index.php?name=$matches[1]\';
使用上述代码,带有/th/slug的帖子工作得非常好,但我的带有/th/page-name-1的页面。。。未找到引发404错误页
So here is the complete story:
首先,我创建了一个父页面;泰语主页“;
https://mywebsite.com/th/在该父页下“;泰语主页“;,我添加了其余的泰语页面,所以结果如下
mywebsite.com/th/page-name-1
mywebsite.com/th/page-name-2
mywebsite.com/th/page-name-3
现在,我想用slug(/th)设置泰国帖子URL,为了实现这一点,我在函数中添加了以下新规则。php
$newrules[\'^th/(.*)$\'] = \'index.php?name=$matches[1]\';
$newrules[\'^se/(.*)$\'] = \'index.php?name=$matches[1]\';
有了上述代码,POST就可以完美地使用语言slug(/th/,/se/)。用/th/page-name-1插入我的页面。。。未找到引发404错误页
Script:
//Create a function to register a new language translation taxonomy
add_action(\'init\',\'nk_add_translation_taxonomy\');
function nk_add_translation_taxonomy(){
global $post;
//set the name of the taxonomy
$taxonomy = \'nk-post-translation\';
//set the types for the taxonomy
$object_type = array(\'post\');
//populate our array of names for our taxonomy
$labels = array(
\'name\' => \'Post Translation\',
\'singular_name\' => \'Post Translation\',
\'search_items\' => \'Search Translation\',
\'all_items\' => \'All Translation\',
\'parent_item\' => \'Parent Translation\',
\'parent_item_colon\' => \'Parent Translation:\',
\'update_item\' => \'Update Translation\',
\'edit_item\' => \'Edit Translation\',
\'add_new_item\' => \'Add New Translation\',
\'new_item_name\' => \'New Translation\',
\'menu_name\' => \'Post Translation\',
);
//define arguments to be used
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'show_ui\' => true,
\'how_in_nav_menus\' => true,
\'public\' => false,
\'show_admin_column\' => true,
\'query_var\' => true,
\'rewrite\' => array(\'slug\' => \'post-translation\')
);
//call the register_taxonomy function
register_taxonomy($taxonomy, $object_type, $args);
}
add_filter( \'post_link\', \'custom_permalink\', 10, 3 );
add_filter( \'rewrite_rules_array\',\'customLangaugeSlugRules\');
add_filter( \'init\',\'flushRules\');
// creating of post permalink from taxnonmy slug
function custom_permalink( $permalink, $post, $leavename ) {
$category = get_the_terms($post->ID,"nk-post-translation");
if ( !empty($category) && $category[0]->slug == "th" )
{
$permalink = trailingslashit( home_url(\'th/\' . $post->post_name ) );
}
elseif ( !empty($category) && $category[0]->slug == "se" )
{
$permalink = trailingslashit( home_url(\'se/\' . $post->post_name ) );
}
else
{
$permalink = trailingslashit( home_url( $post->post_name ) );
}
return $permalink;
}
// using because of its flush the exixsting rules of taxnonmy slug rules
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
// inserting new rules of taxnonmy slug
function customLangaugeSlugRules($rules)
{
$newrules = array();
$newrules[\'^se/(.*)$\'] = \'index.php?name=$matches[1]\';
$newrules[\'^th/(.*)$\'] = \'index.php?name=$matches[1]\';
return $newrules + $rules;
}