最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How does Permalink work with the AMP plugin after Removing Parent Permalink Catalog from Posts

programmeradmin5浏览0评论

I have found some solutions, but they are not perfect with the AMP plugin. For example, I found that this code is completely true for posts that are not amp, but when viewed by url with amp, it reverts to non-amp. here

add_filter( 'post_link', 'remove_parent_category', 10, 3 );
function remove_parent_category( $permalink, $post, $leavename ) {

     $cats = get_the_category( $post->ID );

     if ( $cats ) {

         foreach ($cats as $cat => $parent) {

           $cat_parent_values[] = $parent->parent; 
         }

         if( array_sum( $cat_parent_values ) > 0 )  {

             $url_array      = parse_url( $permalink );
             $url_path       = array_filter( explode( '/', $url_array['path'] ) );
             $url_path      = array_values($url_path);
             $url_path_count = count( $url_path );

             $name_slot      = $url_path_count - 1;  
             $child_cat_slot = $name_slot - 1;

             $new_url_path[] = '/'. $url_path[ $child_cat_slot ];
             $new_url_path[] = '/'. $url_path[ $name_slot ] .'/';

             $new_permalink  = $url_array['scheme'] .'://'. $url_array['host'] . $new_url_path[0] . $new_url_path[1];

         }//end if sum > 0
     }//end if $cats

    return $new_permalink;
}  

Simply speaking i want to do something like this. with non-AMP

/

with mobile and AMP

/

but when accessing /amp/, the url returns to non-amp. The code works perfectly, but it doesn't work with the AMP plugin. Can someone fix this code to work with AMP?

发布评论

评论列表(0)

  1. 暂无评论