Category with the same slug

时间:2014-03-15 作者:Francesco Craparo

我想这样做:

www.website.ext/ciccio/pro
www.website.ext/mario/pro
www.website.ext/giuseppe/pro
为什么我不能那样做?我知道。。段塞/子类别“pro”相同,但在不同的父类别中。所以,我不知道我能做什么/

我试过了,但没有成功,因为每次wordpress都会将类别slug改为:“child\\u category-parent\\u category”。

谢谢

1 个回复
SO网友:Francesco Craparo

我发现了这个代码,我认为这是一个好主意:

add_filter( \'post_link\', \'remove_parent_cats_from_link\', 10, 3 );
function remove_parent_cats_from_link( $permalink, $post, $leavename )
{
$cats = get_the_category( $post->ID );
if ( $cats ) {
// Make sure we use the same start cat as the permalink generator
usort( $cats, \'_usort_terms_by_ID\' ); // order by ID
$category = $cats[0]->slug;
if ( $parent = $cats[0]->parent ) {
// If there are parent categories, collect them and replace them in the link
$parentcats = get_category_parents( $parent, false, \'/\', true );
// str_replace() is not the best solution if you can have duplicates:
// myexamplesite.com/luxemburg/luxemburg/ will be stripped down to myexamplesite.com/
// But if you don\'t expect that, it should work
$permalink = str_replace( $parentcats, \'\', $permalink );
}
}
return $permalink;
}
但我有个麻烦。它不适用于此结构:

www.domain.com/taco/promotions-taco/2014/title-post/

它变成:

www.domain.com/promotions-2014/title-post/ (为什么?)

但我想要这样的结构:

www.domain.com/promotions-taco/2014/title-post/

有可能吗?

结束

相关推荐

Advanced permalinks structure

我想得到这个:mysite.com/category/tag/ 示例:mysite.com/news/sports/ 新闻是一个类别,体育是一个标签我该怎么做?谢谢