How to add /blog/ in front of your single post URLs in WordPress

How to add /blog/ in front of your single post URLs in WordPress
63 Views

when we want to add /blog/ only for wordpress default post, add Following code in your current themes functions.php File.

post_type == ‘post’ ) {
return home_url( ‘/blog/’ . $post->post_name );
}
return $post_link;
}

you have to add below code also.

add_action( ‘generate_rewrite_rules’, ‘generate_rewrite_rules’,99,1 );

function generate_rewrite_rules( $wp_rewrite ) {
$new_rules = array(
‘(.?.+?)/page/?([0-9]{1,})/?$’ => ‘index.php?pagename=$matches[1]&paged=$matches[2]’,
‘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;
}

?>

Was this article helpful?
YesNo

Leave a comment

Your email address will not be published.