自定义分类和自定义帖子类型URL冲突

时间:2014-10-01 作者:Jack Torris

我有一个严重的url问题。

news 自定义帖子类型。是slug是news.

新闻分类slug:news_category

新闻分类url为:http://domain.com/news_category/press-release

我想更改news_category 至新闻news .

所以新闻分类的url应该是:

             http://domain.com/news/press-release
我试着重写那首歌news_categorynews 但这造成了冲突问题。news显示新闻url,但所有新闻帖子类型的帖子都在返回404 not found

新闻帖子的url:

     http://domain.com/news/POST_TITLE
由于帖子url中还包含新闻slug这两个冲突。获取404错误。

有可能修复它吗?我怎样才能修复它?

任何帮助都将不胜感激。

1 个回复
SO网友:Imran Javed

请尝试此代码。这可能对你有帮助。

 class Tax_Seo_Perma {

  public function __construct() {
    // Hooks
    add_filter(\'rewrite_rules_array\',array($this, \'create_rewrite_rules\'));
    add_filter(\'wp_loaded\', array($this, \'flush_rules\'));
    add_filter(\'post_link\', array($this, \'modify_link_addresses\'), 10, 3);
    //add_filter(\'post_type_link\', \'tsp_write_link_addresses\', 10, 3);
}

public function flush_rules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules(); 
}

public function create_rewrite_rules($rewrite) {
    global $wp_rewrite;

    // loop through custom taxonomies
    $args = array(
        \'public\'   => true,
        \'_builtin\' => false 
    );
    $output             = \'names\'; // or objects
    $operator           = \'and\'; // \'and\' or \'or\'
    $custom_taxonomies  = get_taxonomies($args, $output, $operator); 
    if ($custom_taxonomies) {
        foreach ($custom_taxonomies as $tax_name ) {
            $tax_token = \'%\'.$tax_name.\'%\';
            $wp_rewrite->add_rewrite_tag($tax_token, \'(.+)\', $tax_name.\'=\');
        }
    }

    // read current permalink structure and set the same structre
    $keywords_rewrite = $wp_rewrite->generate_rewrite_rules($wp_rewrite->root.$wp_rewrite->permalink_structure);
    return ( $rewrite + $keywords_rewrite );
}

public function modify_link_addresses($permalink, $post_id, $leavename)
{
    global $blog_id;
    global $wp_rewrite;
    // this is user\'s permalink structure set in options
    $permastruct = $wp_rewrite->permalink_structure;

    $args = array(
        \'public\'   => true,
        \'_builtin\' => false 
    );
    $output             = \'names\'; // or objects
    $operator           = \'and\'; // \'and\' or \'or\'
    $custom_taxonomies  = get_taxonomies($args, $output, $operator);

    if ($custom_taxonomies) {
        foreach ($custom_taxonomies as $tax_name ) {
            $tax_token = \'%\'.$tax_name.\'%\';

            $tax_terms = get_the_terms( $post->id, $tax_name );
            //var_dump($tax_terms);
            if ( !empty($tax_terms) )
            {
                foreach($tax_terms as $a_term)
                {
                    $long_slug = $a_term->slug;
                    if( false != (int)$a_term->parent ) { // a\'s parent p exists
                        $p_term = get_term( (int)$a_term->parent, $tax_name );//var_dump($p_term->slug);
                        $long_slug = ($p_term->slug) ? $p_term->slug .\'+\'. $long_slug : $long_slug;
                    } 
                    if( false != (int)$a_term->parent AND false != (int)$p_term->parent ) { // p\'s parent g exists
                        $g_term = get_term( (int)$p_term->parent, $tax_name );
                        $long_slug = ($g_term->slug) ? $g_term->slug .\'+\'. $long_slug : $long_slug;
                    }

                    $permalink = str_replace($tax_token, $long_slug, $permalink);
                    break;
                }
            } else {$permalink = str_replace($tax_token, \'no-\'.$tax_name, $permalink); }
        }
    }

    return $permalink;
 }
 }

结束