Skip to content

Instantly share code, notes, and snippets.

@loorlab
Last active September 17, 2021 15:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loorlab/c46e57bb3298d0397588248ec8e04d0f to your computer and use it in GitHub Desktop.
Save loorlab/c46e57bb3298d0397588248ec8e04d0f to your computer and use it in GitHub Desktop.
<?php
// functions.php
/**
* Rewrite /blog postname
*/
function 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', 'add_rewrite_rules');
function 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', 'change_blog_links', 1, 3);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment