【发布时间】:2017-05-27 21:27:28
【问题描述】:
以下代码允许我删除我的主域帖子的父类别。 我的问题是我正在使用第三方插件来映射我的作者姓名中的第二个域。
- domain1.com 主域
- domain2.com 映射的域
固定链接结构:
- domain1.com/me/cat/subcat/postname => domain2.com/cat/subcat/postname
所以基本上,domain1.com/me 映射到 domain2.com 并且它以这种方式工作得很好
但是如果我删除 cat slug 并只让 subcat 像这样(使用脚本):
- domain1.com/me/subcat/postname 工作中
- domain2.com/subcat/postname 不工作 (ERR_TOO_MANY_REDIRECTS)
删除 URL 中父 slug 的脚本
add_filter( 'post_link', 'remove_parent_category', 10, 3 );
function remove_parent_category( $permalink, $post, $leavename )
{
$cats = get_the_category( $post->ID );
if ( $cats ) {
usort( $cats, '_usort_terms_by_ID' );
$category = $cats[0]->slug;
if ( $parent = $cats[0]->parent ) {
// Find parent categories and replace them in the link
$parentcats = get_category_parents( $parent, false, '/', true );
$permalink = str_replace( $parentcats, '', $permalink );
}
}
return $permalink;
}
【问题讨论】:
标签: wordpress categories slug