Skip to content

Instantly share code, notes, and snippets.

@WordBits
Created September 27, 2018 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WordBits/38625b8e6e46d98961c3cd02211eee93 to your computer and use it in GitHub Desktop.
Save WordBits/38625b8e6e46d98961c3cd02211eee93 to your computer and use it in GitHub Desktop.
This will print /blog/post-name instead of /post-name for posts
<?php
// Download this snippet as a plugin and more at: https://wordbits.io/snippet/add-url-prefix-for-blog-posts/
// Created by: Mark Vi
?>
<?php
/**
* 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
*/
add_action( 'init', 'redefine_post', 1 );
function redefine_post() {
register_post_type( 'post', array(
'labels' => array(
'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
),
'public' => true,
'_builtin' => false,
'_edit_link' => 'post.php?post=%d',
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array( 'slug' => 'blog' ),
'query_var' => false,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment