自定义分类固定链接中的更改导致另一自定义分类的404错误

时间:2013-05-28 作者:Noman

在此帮助下,我创建了基于层次结构的自定义永久链接link, 但现在它在站点中使用的另一个自定义分类法上导致了404错误。我还希望在这个其他分类法上使用自定义永久链接,但如果我不使用任何重写参数并将其保留在默认永久链接上,它仍然不起作用。

第二种分类法的代码:

register_post_type( 
    \'gallery\',
    array(
        \'public\'               => true,
        \'menu_position\'        => 15,
        \'supports\'             => array( \'title\', \'editor\', \'comments\', \'thumbnail\' ),
        \'taxonomies\'           => array( \'\' ),
        \'menu_icon\'            => get_template_directory_uri() . \'/img/icon_gallery.png\',
        \'register_meta_box_cb\' => \'add_gallery_metaboxes\'
    )
);
register_taxonomy(
    \'gallery_category\',
    \'gallery\',
    array(
        \'hierarchical\' => true,
        \'labels\'       => $labels
    )
);
这是我的第一个自定义分类代码:

register_post_type( 
    \'article\',
    array(
        \'public\'        => true,
        \'hierarchical\'  => true,
        \'menu_position\' => 15,
        \'supports\'      => array( \'title\', \'editor\', \'comments\', \'thumbnail\' ),
        \'taxonomies\'    => array( \'\' ),
        \'menu_icon\'     => get_template_directory_uri() . \'/img/icon_article.png\',
        \'query_var\'     => true,
        \'rewrite\'       => array(
            \'slug\'       => \'%article_category%/articles\',
            \'with_front\' => true
        ),
        \'has_archive\'          => \'%article_category%\',
        \'register_meta_box_cb\' => \'add_article_metaboxes\'
    )
);
register_taxonomy(
    \'article_category\',
    \'article\',
    array(
        \'hierarchical\' => true,
        \'labels\'       => $labels,
        \'query_var\'    => true,
        \'rewrite\'      => array( \'slug\' => \'\', \'hierarchical\' => true ),
    )
);
自定义永久链接的函数:

function filter_post_type_link( $link, $post ) {
    if ( $post->post_type != \'article\' )
        return $link;
    if ( $cats = get_the_terms( $post->ID, \'article_category\' ) ) {
        $link = str_replace( \'%article_category%\', get_taxonomy_parents( array_pop( $cats )->term_id, 
            \'article_category\', false, \'/\', true ), $link ); // See custom function defined below
    }
    return $link;
}
add_filter( \'post_type_link\', \'filter_post_type_link\', 10, 2 );

1 个回复
最合适的回答,由SO网友:Noman 整理而成

经过多次努力,我已经解决了这个问题,但这并不完全是我想要的,因为我不得不在一件事上妥协,并在URL中传递了一些分隔符。示例:

example.com/separator1/parentatx/childtax/separator2/postname/
为了避免404错误,在分页和其他自定义分类法的情况下,我创建了一些重写规则。这是我的代码:

第一个帖子类型和自定义分类注册码:

register_post_type( 
    \'article\',
    array(
        \'public\'        => true,
        \'hierarchical\'  => true,
        \'menu_position\' => 15,
        \'supports\'      => array( \'title\', \'editor\', \'comments\', \'thumbnail\' ),
        \'taxonomies\'    => array( \'\' ),
        \'menu_icon\'     => get_template_directory_uri().\'/img/icon_article.png\',
        \'query_var\'     => true                              
        \'rewrite\'       => array(
            \'slug\'       => \'articles/%article_category%/topic/\',
            \'with_front\' => true
        ),
        \'has_archive\'          => \'/all-articles/\',
        \'register_meta_box_cb\' => \'add_article_metaboxes\'
    )
);
register_taxonomy( 
    \'article_category\',
    \'article\',
    array(
        \'hierarchical\' => true,
        \'labels\'       => $labels,
        \'query_var\'    => true,
        \'rewrite\'      => array( \'slug\' => \'/articles/\', \'hierarchical\' => true ),
    )
);
在第一个自定义分类法中,我使用“articles”和“topic”作为分隔符;您可以根据您的场景使用任意选项。

第二个帖子类型和自定义分类注册码:

register_post_type( 
    \'gallery\',
    array(
        \'public\'        => true,
        \'menu_position\' => 15,
        \'supports\'      => array( \'title\', \'editor\', \'comments\', \'thumbnail\' ),
        \'taxonomies\'    => array( \'\' ),
        \'menu_icon\'     => get_template_directory_uri().\'/img/icon_gallery.png\',
        \'query_var\'     => true,
        \'rewrite\'       => array(
            \'slug\'       => \'galleries/%gallery_category%/images/\',
            \'with_front\' => true
        ),
        \'has_archive\'          => \'/all-galleries/\',
        \'register_meta_box_cb\' => \'add_gallery_metaboxes\'
    )
);
在第二个自定义分类法中,我使用“图库”和“图像”作为分隔符;您可以根据您的场景使用任意选项。

重写规则的功能:

add_filter( \'rewrite_rules_array\', \'mmp_rewrite_rules\' );
function mmp_rewrite_rules( $rules ) {
    $newRules  = array();
    //Rules for First Taxonomy
    $newRules[\'articles/(.+)/(.+)/(topic)/(.+)/?$\'] = \'index.php?article=$matches[4]\'; 
    $newRules[\'articles/(.+)/(topic)/(.+)/?$\'] = \'index.php?article_category=$matches[1]&article=$matches[3]\';
    $newRules[\'articles/(.+)/(.+)/?([0-9]{1,})/?$\'] = \'index.php?article_category=$matches[1]&page=$matches[3]\';
    $newRules[\'articles/(.+)/(.+)/(page)/?([0-9]{1,})/?$\'] = \'index.php?article_category=$matches[1]&page=$matches[4]\';
    $newRules[\'articles/(.+)/?$\'] = \'index.php?article_category=$matches[1]\';
    //Rules for Second Taxonomy
    $newRules[\'galleries/(.+)/(.+)/(images)/(.+)/?$\'] = \'index.php?gallery=$matches[4]\'; 
    $newRules[\'galleries/(.+)/(images)/(.+)/?$\'] = \'index.php?gallery_category=$matches[1]&gallery=$matches[3]\';
    $newRules[\'galleries/(.+)/(.+)/?([0-9]{1,})/?$\'] = \'index.php?gallery_category=$matches[1]&page=$matches[3]\';
    $newRules[\'galleries/(.+)/(.+)/(page)/?([0-9]{1,})/?$\'] = \'index.php?gallery_category=$matches[1]&page=$matches[4]\';
    $newRules[\'galleries/(.+)/?$\'] = \'index.php?gallery_category=$matches[1]\'; 

    return array_merge( $newRules, $rules );
}
根据自定义分类层次结构更改自定义帖子类型URL的函数:

function filter_post_type_link( $link, $post ) {
    if ( $post->post_type != \'article\' && $post->post_type != \'gallery\' )
        return $link;       
    if ( $post->post_type == \'article\' ) {
        if ( $cats = get_the_terms( $post->ID, \'article_category\' ) ) {
            $link = str_replace( \'%article_category%\', get_taxonomy_parents( array_pop( $cats )->term_id, 
                \'article_category\', false, \'/\', true ), $link ); // See custom function defined below
        }
        return $link;
    } elseif ( $post->post_type == \'gallery\' ) {
        if ( $cats = get_the_terms( $post->ID, \'gallery_category\' ) ) {
            $link = str_replace(\'%gallery_category%\', get_taxonomy_parents( array_pop( $cats )->term_id, 
                \'gallery_category\', false, \'/\', true ), $link); // See custom function defined below
        }
        return $link;
    }
}
add_filter( \'post_type_link\', \'filter_post_type_link\', 10, 2 );

// My own function to do what get_category_parents does for other taxonomies
function get_taxonomy_parents( $id, $taxonomy, $link = false, $separator = \'/\', $nicename = false, $visited = array() ) {
    $chain = \'\';   
    $parent = &get_term( $id, $taxonomy );
    if ( is_wp_error( $parent ) ) {
        return $parent;
    }
    if ( $nicename )    
        $name = $parent -> slug; 
    else    
        $name = $parent -> name;
    if ( $parent -> parent && ( $parent -> parent != $parent -> term_id ) && !in_array( $parent -> parent, $visited ) ) {
        $visited[] = $parent -> parent;    
        $chain .= get_taxonomy_parents( $parent -> parent, $taxonomy, $link, $separator, $nicename, $visited );
    }
    if ( $link ) {
        // Nothing, can\'t get this working :(
    } else {   
        $chain .= $name . $separator;    
    }
    return $chain;    
}

结束

相关推荐

Taxonomy based permalinks

在我的网站上,我使用自己的分类法。我希望分类学与permalink相结合。因此,如果一个新闻页面有分类法,permalink就会改变。像这样:mysite。com/%custom\\u tax%/blog/%postname%,如果是一个页面,请点击:mysite。com/%custom\\u tax%/%postname%但如果没有我想要的分类法:mysite。com/blog/%postname%,如果它是一个页面:mysite。com/%postname%我怎样才能轻松地做到这一点?我已经设置了%c