新主题的WordPress固定链接格式是:/%category%/%post_id%.html(分类名/文章ID.html)
但是我有的分类下有很多子分类,那么文章链接就会变成:https://www.xintheme.com/父分类/子分类/文章ID.html
这样的话链接目录层次就有点深,从某种方面来讲不太利于SEO优化,然后就要想办法干掉WordPress固定链接中的子分类了,把下面的代码添加到WordPress主题中的functions.php里面:
[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);
function custom_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( $cats as $cat ) {
$cat = get_category($cat->term_id);
//if($cat->parent) { $subcats[] = sanitize_title($cat->name);
if($cat->parent) { $subcats[] = $cat->slug;}}
if($subcats) {
foreach($subcats as $subcat) {
$subcat = $subcat.'/';
$permalink = str_replace($subcat, "", $permalink);}}
break;}
return $permalink;} [/php]
多级分类解决方法:
[php]// wordpress 去掉固定链接中的所有子分类包含孙分类https://www.xintheme.com/theme/blog/ceshi/2294.html
//去掉后: https://www.xintheme.com/theme/2294.html
function remove_child_categories_from_permalinks( $category ) {
while ( $category->parent ) {
$category = get_term( $category->parent, 'category' );
}
return $category;
}
add_filter( 'post_link_category', 'remove_child_categories_from_permalinks' );[/php]
本文《WordPress去除固定链接中的子分类标签》由网友投稿或:「admin」整理自网络。
转载请声明来自:云猴子 - https://www.yunhouzi.com/55.html
1,本站所有资源均来源于用户上传或整理与网络,如有侵权请【内容投诉】删除,我们将及时处理!
2,本站资源仅供大家学习和交流,请不要用于商业用途,下载后请于24小时后删除!
3,如果你也有好的资源,可以投稿到本站,有金币奖励和额外的收入!
4,从您进入本站开始,已表示您已同意接受本站【版权声明】中的一切条款!
6,申明:本站资源出售只是赞助,仅用于本站服务器和日常运营所需!不提供任何技术支持。
7,如压缩包提示有密码,www.yunhouzi.com
云猴子 » WordPress去除固定链接中的子分类标签