404错误-为帖子添加自定义规则后的页面问题

时间:2021-01-15 作者:Puzzled Boy

我正在尝试为我的网站创建自定义翻译。

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

1 个回复
SO网友:Sally CJ

发生原因

上的错误404th/<page slug> 发生页面是因为自定义重写规则覆盖默认页面规则,否则会加载正确的页面(具有slug<page slug>).

如果permalink结构是/%postname%, i、 e。example.com/<post slug>, 然后,通过使用post_rewrite_rules 钩子代替rewrite_rules_array.

因此,您需要做的就是:

// Change this:
add_filter( \'rewrite_rules_array\',\'customLangaugeSlugRules\');

// to this one:
add_filter( \'post_rewrite_rules\',\'customLangaugeSlugRules\');
然后刷新重写规则-但作为WP_Rewrite::flush_rules() documentation 表示:(*略微重新格式化)

因为此功能在性能方面可能会非常昂贵,所以应该尽可能少地使用它,例如在插件或主题的激活或停用期间。每次尝试都应该避免在每个页面加载上执行的挂钩中使用它,例如init.

因此,请记住这一点,您只需转到永久链接设置管理页面,就可以手动刷新规则(wp-admin → 设置→ Permalink)。

相关推荐

multi-language WordPress site

我正在制作由WordPress支持的多语言网站。WPML 不是免费的,而且它对数据库进行了太多额外的sql查询,所以对于我的站点来说,这不是一个好的解决方案。qTranslate 将所有语言保存在一个数据库行中,以后移动到另一个多语言插件非常复杂。<?php _e( \'<!--:en-->english text<!--:--><!--:de-->german text<!--:-->\' ); ?> xLanguage 两年多没有更新