So I have website on a WordPress engine. And I want to make posts urls looks like http://website.com/blog/post-name/ or http://website.com/blog/categ-name/post-name/. But everything else will look like http://website.com/page-name/. Just want to add prefix 'blog' to urls for all posts but not pages. My permalink settings is /%postname%/. If i will put /blog/%postname%/ I will have 'blog' prefix in every URL of my site :( I can't find solution in WordPress admin backend and I didn't find any acceptable plugin for this. Any help is appreciated, thanks.
5 Answers
1) Add this rewrite at the end of you function.php
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);
2) Go to Settings > Permalinks and click Save Changes
.
-
It seems that you cannot edit the post slug anymore, after using this solution.– MarianCommented Sep 26, 2022 at 15:28
Create a category called blog
and use the permalink structure /%category%/%postname%/
.
-
Thanks, I thought about this way too. But I have a couple of categories, and I don't wanna to have 'blog' in cats list. Sure, I can hide the 'blog' cat with some hacks. But maybe there is more elegant solution. Commented Sep 3, 2011 at 10:00
-
hi, did you find the solution for this, I also have the same problem– EdesaCommented Aug 5, 2012 at 7:03
Go to Settings > Permalinks, choose "Custom Structure" and rewrite this field to "/blog/%postname%/". Your post will get /blog/ prefix, but your pages doesn't. I've tested it at Wordpress 5.2.2
-
3Problem with this solution is that it will add
blog
prefix in front of everything. Every CPT. Commented Nov 7, 2019 at 17:38 -
3@ZdenekHatak if a CPT is registered with the option
'rewrite' => array( 'with_front' => false )
then it should be OK. Docs forwith_front
flag: "(bool) Whether the permastruct should be prepended with WP_Rewrite::$front. Default true.". See: developer.wordpress.org/reference/functions/register_post_type– firxworxCommented Mar 18, 2020 at 5:30
I did this:
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' ),
) );
}
It involves creating a custom post type (which is slightly annoying), but it overrides and works with post.
I wish there was a better way of doing this! (Otherwise yes you get /blog/ in front of everything if you just change the permalinks).
function golden_oak_web_design_blog_generate_rewrite_rules( $wp_rewrite ) {
$new_rules = array(
'(([^/]+/)*blog)/page/?([0-9]{1,})/?$' => 'index.php?pagename=$matches[1]&paged=$matches[3]',
'blog/([^/]+)/?$' => 'index.php?post_type=post&name=$matches[1]',
'blog/[^/]+/attachment/([^/]+)/?$' => 'index.php?post_type=post&attachment=$matches[1]',
'blog/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?post_type=post&attachment=$matches[1]&tb=1',
'blog/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]',
'blog/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]',
'blog/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]',
'blog/[^/]+/attachment/([^/]+)/embed/?$' => 'index.php?post_type=post&attachment=$matches[1]&embed=true',
'blog/[^/]+/embed/([^/]+)/?$' => 'index.php?post_type=post&attachment=$matches[1]&embed=true',
'blog/([^/]+)/embed/?$' => 'index.php?post_type=post&name=$matches[1]&embed=true',
'blog/[^/]+/([^/]+)/embed/?$' => 'index.php?post_type=post&attachment=$matches[1]&embed=true',
'blog/([^/]+)/trackback/?$' => 'index.php?post_type=post&name=$matches[1]&tb=1',
'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&name=$matches[1]&feed=$matches[2]',
'blog/page/([0-9]{1,})/?$' => 'index.php?post_type=post&paged=$matches[1]',
'blog/[^/]+/page/?([0-9]{1,})/?$' => 'index.php?post_type=post&name=$matches[1]&paged=$matches[2]',
'blog/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?post_type=post&name=$matches[1]&paged=$matches[2]',
'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?post_type=post&name=$matches[1]&cpage=$matches[2]',
'blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?post_type=post&name=$matches[1]&page=$matches[2]',
'blog/[^/]+/([^/]+)/?$' => 'index.php?post_type=post&attachment=$matches[1]',
'blog/[^/]+/([^/]+)/trackback/?$' => 'index.php?post_type=post&attachment=$matches[1]&tb=1',
'blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]',
'blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]',
'blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]',
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'golden_oak_web_design_blog_generate_rewrite_rules' );
function golden_oak_web_design_update_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', 'golden_oak_web_design_update_post_link', 1, 3 );
-
Paste the above code snippet into the functions.php file of your active parent theme or child theme. Reference Link: goldenoakwebdesign.com/blog/… Commented Nov 17, 2020 at 10:20
-
Once you pasted the above code. Go to Settings --> Permalinks and click Save Changes. Commented Nov 17, 2020 at 10:23