0

Currently when creating an entry in this wordpres the URL is :

http://www.sitio.com/entrada1

But you could customize it so that it reads:

http://www.sitio.com/blogs/entrada1

PS : Making it does not affect my other URLs Custom Post Type Created or Pages, only want to change the default entries for wordpress

Thank you

2 Answers2

2

Add following code at the end of your theme functions.php file. If your theme functions.php file has ?> at the end then paste the code just before ?> Otherwise just paste it. After that update permalink from wordpress admin.

    /**
     * Add new rewrite rule
     */
    function create_new_url_querystring() {
        add_rewrite_rule(
            'blog/([^/]*)$',
            'index.php?name=$matches[1]',
            'top'
        );
        add_rewrite_tag('%blog%','([^/]*)');
    }
    add_action('init', 'create_new_url_querystring', 999 );
    /**
     * Modify post link
     * This will print /blog/post-name instead of /post-name
     */
    function append_query_string( $url, $post, $leavename ) {
        if ( $post->post_type == 'post' ) {     
            $url = home_url( user_trailingslashit( "blog/$post->post_name" ) );
        }
        return $url;
    }
    add_filter( 'post_link', 'append_query_string', 10, 3 );
    /**
     * Redirect all posts to new url
     * If you get error 'Too many redirects' or 'Redirect loop', then delete everything below
     */
    function redirect_old_urls() {
        if ( is_singular('post') ) {
            global $post;
            if ( strpos( $_SERVER['REQUEST_URI'], '/blog/') === false) {
               wp_redirect( home_url( user_trailingslashit( "blog/$post->post_name" ) ), 301 );
               exit();
            }
        }
    }
    add_filter( 'template_redirect', 'redirect_old_urls' );
0

There are two methods to do this,

Method 1

// Update the post-type post and then add your custom slug
add_action( 'init', 'update_default_post_slug', 1 );
function update_default_post_slug() {
     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' => 'blogs' ),
        'query_var' => false,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
    ) );
}

Method 2

Check this: Different permalink structure for blog posts than pages in Wordpress?

I hope this helps.

Community
  • 1
  • 1
Nikhil
  • 1,450
  • 11
  • 24
  • The method 1 its work but when the url shows "blogs/" the url don't exist, appear error 404... –  Jan 14 '16 at 19:29