WordPress固定链接格式是:/%category%/%post_id%.html(分类名/文章ID.html)
但是我有的分类下有很多子分类,那么文章链接就会变成:https://www.xintheme.com/父分类/子分类/文章ID.html
这样的话链接目录层次就有点深,从某种方面来讲不太利于SEO优化,然后就要想办法干掉WordPress固定链接中的子分类了,把下面的代码添加到WordPress主题中的functions.php里面:
//wordpress去掉固定链接中的子分类https://www.xintheme.com/theme/blog/2294.html
//去掉后:https://www.xintheme.com/theme/2294.html
add_filter('post_link','custom_post_type_link',10,3);
functioncustom_post_type_link($permalink,$post,$leavename){
if(!gettype($post)=='post'){
return$permalink;}
switch($post->post_type){
case'post':
//$permalink=get_home_url().'/'.$post->post_name.'/';
$cats=get_the_category($post->ID);
$subcats=array();
foreach($catsas$cat){
$cat=get_category($cat->term_id);
//if($cat->parent){$subcats[]=