Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL prefix for posts WordPress

Tags:

wordpress

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.

like image 509
Yaroslav Rogoza Avatar asked Sep 02 '11 15:09

Yaroslav Rogoza


People also ask

What is URL prefix?

A Search Console property that, as defined, includes the protocol (http or https) and can include a path string when you created the property. You can see the property URL in the property selector dropdown. Examples of URL-prefix properties: https://m.example.com/petstore/

How do I change the custom post URL in WordPress?

Changing Custom Post Type Permalinks in WordPress Upon activation, you need to visit Settings » Permalinks page and scroll down to 'Permalink Settings for Custom Post Types' section. Here you can change the permalink structure of single items in your custom post type.

How do I change the category and tag URL prefix in WordPress?

You need to visit the Settings » Permalinks page from your WordPress dashboard and scroll down to the 'Optional' section. In the 'Category base' field, you can enter the prefix you would like to use next to the category base option. You can also change the tag base prefix if you want.

How do I set the URL in WordPress?

SImply login to your WordPress dashboard and go to Settings » General. Here, you can change your WordPress site URLs in the 'WordPress Address' and 'Site Address' boxes. For most websites, these will be the same URL. After that, click the 'Save Changes' button to save your URL changes.


1 Answers

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).

like image 169
timhc22 Avatar answered Oct 10 '22 01:10

timhc22