Permalinks Question: Adding a prefix ONLY in front of the posts

I want all my posts to have /ARTICLES/%post-name% structure.

But as soon as I modify the custom structure to that, for a reason that beats me, wordpress adds the articles prefix across the board.

So I end up with urls like this for my category pages with something like the following

mysite.com/articles/category/videos

here the videos is a category and since I did not change the category to anything else, I was expecting the urls to be well, mysite.com/category/videos ), not mysite.com/articles/category/whatever
Obviously, such a URL does not make sense.

On the permalinks screen, under the Common Settings‘s Post name option, I get a grayed out ( uneditable ) sample link that shows something like mysite.com/sample-post.

I wished I could have edit that grayed out info

to mysite.com/articles/sample-post while keeping the other options as is.

But this does not seem to be possible.

Am I at a dead-end here?

Is there a way to only change the post urls without effecting the other urls?

4 Answers
4

1) Add this rewrite at the end of you function.php

function add_rewrite_rules( $wp_rewrite )
{
    $new_rules = array(
        'YOUR_PREFIX/(.+?)/?$' => '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('/YOUR_PREFIX/'. $post->post_name."https://wordpress.stackexchange.com/");
    }

    return $post_link;
}
add_filter('post_link', 'change_blog_links', 1, 3);

2) Go to Settings > Permalinks and click Save Changes.

Leave a Comment