admin管理员组文章数量:1127084
I want to customize the permalinks structure for posts in WordPress based on their categories but only posts of one category,
The permalinks of the "android" posts category become like these:
/
and other category posts no change:
/
I try below code but get 404:
I do "Flush Permalinks"
function custom_permalinks_structure($permalink, $post, $leavename) {
if ($post->post_type == 'post' && has_category('android', $post)) {
$permalink = home_url('/android/' . $post->post_name . '/');
} elseif ($post->post_type == 'post') {
$permalink = home_url('/' . $post->post_name . '/');
}
return $permalink;
}
add_filter('post_link', 'custom_permalinks_structure', 10, 3);
add_filter('post_type_link', 'custom_permalinks_structure', 10, 3);
I try the below code too :
add_filter( 'post_link', 'pl_custom_permalink', 10, 3 );
function pl_custom_permalink( $permalink, $post, $leavename ) {
if ( has_category( 'android', $post->ID) ) {
$permalink = trailingslashit( home_url('/android/'. $post->post_name . '/' ) );
}
return $permalink;
}
add_action('init', 'pl_custom_rewrite_rules');
function pl_custom_rewrite_rules( $wp_rewrite ) {
add_rewrite_rule( '^new_slug/(.*)$', 'index.php?name=$matches[1]', 'top' );
}
add_action( 'wp', function( $wp ){
if ( ! preg_match( '#^android/#', $wp->request ) &&
is_singular() && in_category( 'android' ) ) {
wp_redirect( get_permalink() );
exit;
}
} );
本文标签: functionsCustom permalink for post of one category
版权声明:本文标题:functions - Custom permalink for post of one category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736691662a1947965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论