Skip to content

Instantly share code, notes, and snippets.

@roscabgdn
Created October 17, 2016 09:54
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 roscabgdn/93e6ad0f15d3d96706eedf9e4d63b5d0 to your computer and use it in GitHub Desktop.
Save roscabgdn/93e6ad0f15d3d96706eedf9e4d63b5d0 to your computer and use it in GitHub Desktop.
Rewrite - add /blog/post-name to posts
/**
* Add new rewrite rule
*/
function create_new_url_querystring() {
add_rewrite_rule(
'blog/([^/]*)$',
'index.php?name=$matches[1]',
'top'
);
add_rewrite_tag('%blog%','([^/]*)');
}
add_action('init', 'create_new_url_querystring', 999 );
/**
* Modify post link
* This will print /blog/post-name instead of /post-name
*/
function append_query_string( $url, $post, $leavename ) {
if ( $post->post_type == 'post' ) {
$url = home_url( user_trailingslashit( "blog/$post->post_name" ) );
}
return $url;
}
add_filter( 'post_link', 'append_query_string', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment