Last active
July 20, 2020 20:41
-
-
Save ChrisLTD/ee618317ea2d8c811647156c8a17ab23 to your computer and use it in GitHub Desktop.
Change slug / permalink of WordPress 'posts' post type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Rewrite blog posts so they start with /blog | |
*/ | |
function change_posts_post_type_args( $args, $post_type ) { | |
if ( $post_type == "post" ) { | |
$args['rewrite'] = array( | |
'slug' => 'blog', | |
'with_front' => false | |
); | |
} | |
return $args; | |
} | |
add_filter( 'register_post_type_args', 'change_posts_post_type_args', 20, 2 ); | |
function update_posts_post_link( $post_link, $id = 0 ) { | |
$post = get_post( $id ); | |
if( is_object( $post ) && $post->post_type == 'post' ) { | |
return home_url( '/blog/' . $post->post_name ); | |
} | |
return $post_link; | |
} | |
add_filter( 'post_link', 'update_posts_post_link', 1, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment