WordPress
October 9, 2021

How to add /blog/ in front of your single post URLs in WordPress

when we want to add /blog/ only for wordpress default post, add Following code in your current themes functions.php File.

post_type == ‘post’ ) {
return home_url( ‘/blog/’ . $post->post_name );
}
return $post_link;
}

you have to add below code also.

add_action( ‘generate_rewrite_rules’, ‘generate_rewrite_rules’,99,1 );

function generate_rewrite_rules( $wp_rewrite ) {
$new_rules = array(
‘(.?.+?)/page/?([0-9]{1,})/?$’ => ‘index.php?pagename=$matches[1]&paged=$matches[2]’,
‘blog/([^/]+)/?$’ => ‘index.php?post_type=post&name=$matches[1]’,
‘blog/[^/]+/attachment/([^/]+)/?$’ => ‘index.php?post_type=post&attachment=$matches[1]’,
‘blog/[^/]+/attachment/([^/]+)/trackback/?$’ => ‘index.php?post_type=post&attachment=$matches[1]&tb=1’,
‘blog/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$’ => ‘index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]’,
‘blog/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$’ => ‘index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]’,
‘blog/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$’ => ‘index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]’,
‘blog/[^/]+/attachment/([^/]+)/embed/?$’ => ‘index.php?post_type=post&attachment=$matches[1]&embed=true’,
‘blog/[^/]+/embed/([^/]+)/?$’ => ‘index.php?post_type=post&attachment=$matches[1]&embed=true’,
‘blog/([^/]+)/embed/?$’ => ‘index.php?post_type=post&name=$matches[1]&embed=true’,
‘blog/[^/]+/([^/]+)/embed/?$’ => ‘index.php?post_type=post&attachment=$matches[1]&embed=true’,
‘blog/([^/]+)/trackback/?$’ => ‘index.php?post_type=post&name=$matches[1]&tb=1’,
‘blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$’ => ‘index.php?post_type=post&name=$matches[1]&feed=$matches[2]’,
‘blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$’ => ‘index.php?post_type=post&name=$matches[1]&feed=$matches[2]’,
‘blog/page/([0-9]{1,})/?$’ => ‘index.php?post_type=post&paged=$matches[1]’,
‘blog/[^/]+/page/?([0-9]{1,})/?$’ => ‘index.php?post_type=post&name=$matches[1]&paged=$matches[2]’,
‘blog/([^/]+)/page/?([0-9]{1,})/?$’ => ‘index.php?post_type=post&name=$matches[1]&paged=$matches[2]’,
‘blog/([^/]+)/comment-page-([0-9]{1,})/?$’ => ‘index.php?post_type=post&name=$matches[1]&cpage=$matches[2]’,
‘blog/([^/]+)(/[0-9]+)?/?$’ => ‘index.php?post_type=post&name=$matches[1]&page=$matches[2]’,
‘blog/[^/]+/([^/]+)/?$’ => ‘index.php?post_type=post&attachment=$matches[1]’,
‘blog/[^/]+/([^/]+)/trackback/?$’ => ‘index.php?post_type=post&attachment=$matches[1]&tb=1’,
‘blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$’ => ‘index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]’,
‘blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$’ => ‘index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]’,
‘blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$’ => ‘index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]’,
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

?>

Related Post


Delete Old Post in WordPress

We want to delete old post in easy way, Add the following code to your current themes functions.php file and reload your page and see your post may have been deleted.

Get product details from $product object WooCommerce

I am publish this article for get values from WooCommerce $product object. You can get all information of product from $product object. When we have $product object. Get $product object by $product_id. Get $order object by $order_id. Get products from $cart object.

How to Create Loop Element Using Visual Composer API

When we are making site with visual composer some time not possible to built some designs using visual composer elements. So at that time we need to use HTML element for built designs. But visual composer API provide facility to built own custom element for some special requirements and its very easy. We can built […]

Add Recaptcha in Custom Form in WordPress

You have to add recaptcha external js so use following function in your current themes functions.php file Set Recaptcha element in your form. //Custom Form Name: Put below js in your current themes custom.js file. //Captcha validation script jQuery(document).ready(function($) { $(document).on(‘submit’,’.wc-captcha-form’,function(e){ if(grecaptcha.getResponse() == “”) { e.preventDefault(); $(‘.captcha-error’).text(“Please verify captcha”); } else { $(‘.captcha-error’).text(“”); } }); […]

WordPress Login Form Without Plugin

Are you need simple login form without plugin for your WordPress website? Yes, Then i have created [wc_login_form] shortcode with quick simple code. So you can put login form anywhere like in Pages, Posts and Widget etc… Therefore you just need to put [wc_login_form] shortcode where you need login form. So you need to follow […]

WooCommerce Redirect to Custom Thank you Page

The below code allows you to redirect to a custom page after the user has successfully completed a purchase when using WooCommerce plugin. Simply add the below code to the active child theme function.php and replace the URL in the function wp_redirect() to the desired page.


Leave a Reply

Your email address will not be published. Required fields are marked *