Created
August 25, 2021 11:43
-
-
Save aslamdoctor/133f2ef600368f3dc8da930b4bfac52b to your computer and use it in GitHub Desktop.
Snippet: Change blog permalinks in WordPress
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 | |
function wpmix_add_rewrite_rules( $wp_rewrite ) | |
{ | |
$new_rules = array( | |
'blog/(.+?)/?$' => 'index.php?post_type=post&name='. $wp_rewrite->preg_index(1), | |
); | |
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules; | |
} | |
add_action('generate_rewrite_rules', 'wpmix_add_rewrite_rules'); | |
function wpmix_change_blog_links($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', 'wpmix_change_blog_links', 1, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment