Nick
Nick

Reputation: 291

How to add blog directory in front of single WordPress post?

I have a WordPress site set up and I would like the blog directory to show in the URL when viewing a single post:

mysite.com/blog/my-first-post instead of mysite.com/my-first-post

Any ideas how to accomplish this?

Upvotes: 2

Views: 10442

Answers (3)

SUNMOON BAPPY
SUNMOON BAPPY

Reputation: 1

Based on Number two answer

Create a Home page and a Blog page (make sure the blog page is named "Blog" and the slug is "blog") Go to Settings>Reading Choose Static Home Page Set your home page and blog page Go to Settings>Permalinks Create a custom structure: /blog/%postname%/ From the WordPress Codex: http://codex.wordpress.org/Making_Your_Blog_Appear_in_a_Non-Root_Folder

If you have a custom post type set the rewrite rule:

'rewrite' => array( 
    'slug' => 'resources/case-study',
    'with_front' => false
 ),

Upvotes: 0

Chris Panayotoff
Chris Panayotoff

Reputation: 1956

I know this question is old, but somebody may benefit from my answer:

There are two parts: adding rules so /blog/article-slug and /blog/paged/ points to the proper URLS and also modifying all slugs to the new links:

function THEME_SLUG_posts_add_rewrite_rules( $wp_rewrite )
{
    $new_rules = [
        'articles/page/([0-9]{1,})/?$' => 'index.php?post_type=post&paged='. $wp_rewrite->preg_index(1),
        'articles/(.+?)/?$' => 'index.php?post_type=post&name='. $wp_rewrite->preg_index(1),
    ];
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'THEME_SLUG_posts_add_rewrite_rules');

function THEME_SLUG_posts_change_blog_links($post_link, $id=0){
    $post = get_post($id);
    if( is_object($post) && $post->post_type == 'post'){
        return home_url('/articles/'. $post->post_name.'/');
    }
    return $post_link;
}
add_filter('post_link', 'THEME_SLUG_posts_change_blog_links', 1, 3);

Here is the example of adding /articles/ before blog posts title. Don't forget to flush the rewrite rules, for example by saving the permalink settings

Upvotes: 9

user2329093
user2329093

Reputation:

  1. Create a Home page and a Blog page (make sure the blog page is named "Blog" and the slug is "blog")
  2. Go to Settings>Reading
  3. Choose Static Home Page
  4. Set your home page and blog page
  5. Go to Settings>Permalinks
  6. Create a custom structure: /blog/%postname%/

From the WordPress Codex: http://codex.wordpress.org/Making_Your_Blog_Appear_in_a_Non-Root_Folder

Upvotes: 3

Related Questions