Jimmy Khanh
Jimmy Khanh

Reputation: 119

How to insert text on post link Wordpress?

only post link current: http://mydomain/post-name I want change only post link : http://mydomain/blog/post-name

Upvotes: 0

Views: 755

Answers (1)

vard
vard

Reputation: 4136

In the permalink page (Settings > Permalinks), select the last option to enter a custom permalink structure and enter this :

/blog/%postname%/

It will prepend all your post urls with /blog/.

UPDATE

To prefix posts only, use the following function in your functions.php :

function add_rewrite_rules( $wp_rewrite ) 
{
    $new_rules = array(
        'blog/(.+?)/?$' => '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('/blog/'. $post->post_name.'/');
    }
    return $post_link;
}
add_filter('post_link', 'change_blog_links', 1, 3);

Upvotes: 3

Related Questions