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

I have found some solutions, but they are not perfect with the AMP plugin. For example, I found that this code is comple

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744831333a4596102.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信