Pages

07 April, 2022

URL prefix for posts WordPress | Use Blog Word Prefix in post only wordpress URL

 

This function use in wordpress function.php file in last .


if you want to change url according your post exp.


https://www.blogger.com/blog/category/postname


"blog" is a prefix 

This is use only post not show in page.

then visite permalink and set fist custome/

category/postname


copy past and check 

1) Add this rewrite at the end of you function.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);

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




Create a category called blog and use the permalink structure /%category%/%postname%/.









2nd 


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

No comments:

Post a Comment

Thanks