如何从子页面的永久链接中删除父段?

Olk*_*afa 3 wordpress

如何从子页面的永久链接中删除父 slug?这可能不是 Atahualpa 特有的,但我不知道该怎么做……也许有一个可以工作的插件?

我使用设置为父页面的子页面的页面(即不是帖子)。在我的菜单栏导航中,它们显示为:

/services/page1.html
/services/page2.html
/services/page3.html
Run Code Online (Sandbox Code Playgroud)

我想要的是:

/page1.html
/page2.html
/page3.html
Run Code Online (Sandbox Code Playgroud)

(我正在使用“html-on-pages”插件来添加 .html,因为我正在从另一台服务器上移动这个站点,这是它目前拥有的站点结构。)

有什么方法可以完成我想要做的事情吗?

Pra*_*eek 5

将此添加到您的functions.php

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;
}
Run Code Online (Sandbox Code Playgroud)