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条)