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

Today we will show you how to add /blog/ in front of your single post URL’s. Let’s take this blog post for example, at the moment you will see the single post is the default ‘how-to-add-blog-in-front-of-your-single-post-urls-in-wordpress’, maybe you or one of your clients has asked you to change this to ‘/blog/how-to-add-blog-in-front-of-your-single-post-urls-in-wordpress’. This can be achieved quite easily.

Adding ‘blog’ to your single posts

Firstly, you will want to go to your Permalinks settings (WP Admin → Settings → Permalinks) and select ‘Custom Structure’. It should display your current structure in the text box next to it. Now, add /blog in front of what is in the text box (including the slash in front!). You can change the word blog if you want something different.

Adding /blog/ to your categories and tags

Next, we’ll also add the new base in front of the category and tag URLs. If the text boxes are empty, add this in the Category base: blog/category and this in the Tag base: blog/tag;

And that’s it. Your blogs will now have blog/ in front of them whilst your other pages remain unchanged!

Custom post types

If you are using Custom Post Types (CPT) on your site, you will need to make sure that ‘rewrite with front’ is disabled for each of your post types. Otherwise your custom post type URLs will also get changed to have /blog in-front of them.

If you use a plugin to register your CPT’s, look for the ‘Rewrite‘ option, and make sure that ‘With Front‘ is unchecked. After saving your settings, go back to the Permalinks Settings and click ‘Save Changes’ (this is necessary so that the new links are re-generated).

If this doesn’t work, try also filling out the ‘Slug’ in your post type ‘Rewrite’ settings. Some plugins require this for the ‘With Front’ option to work (and again, save your permalink settings!).

If you have created your own CPT’s in your code, make sure that when registering your post type, you have disabled rewrite with front. As an example, if you had a CPT for Events you will need to add the following code:


'rewrite'	      	=> array( 
	'slug' => 'events',
	'with_front' => false
),

As always, after making this change, save your permalink settings in WP Admin after changing your rewrite rules!

We hope you found this useful… happy coding!

Nathan da Silva - Profile

Posted by: Nathan da Silva

Nathan is the Founder of Silva Web Designs. He is passionate about web development, website design and basically anything digital related. His main expertise is with WordPress, Magento, Shopify as well of many other frameworks. Whether you need responsive design, SEO, speed optimisation or anything else in the world of digital then get in touch. If you would like to work with Nathan, simply drop him an email at [email protected]

It’s good to share

Adding ACF Fields as Admin Columns to your CPT

We all know how awesome Advanced Custom Fields (ACF) is right? We pretty much use the Pro version of it on every WordPress website build we do.

Today, we are going to show you how to add fields to your CPT (Custom Post Type) to the backend Admin Columns.

There are a few plugins that can accomplish adding your ACF fields as admin columns to the backend. Admin Columns is one of the best ones that does the trick. The paid version allows your custom post type to connect with ACF Pro.

As programmers, we want to keep the number of plugins we use to a minimum right? well, it’s pretty simple to add these manually!

Okay, so here we go… Let’s say that you have created a post type, ‘hosting’, and two custom meta fields, ‘start_date’ and ‘end_date’. You’d like to add both meta fields to the custom post type list view. First of all, we will need to add the following to the functions.php file:-


/**
 *	ACF Admin Columns
 *
 */

 function add_acf_columns ( $columns ) {
   return array_merge ( $columns, array ( 
     'start_date' => __ ( 'Starts' ),
     'end_date'   => __ ( 'Ends' ) 
   ) );
 }
 add_filter ( 'manage_hosting_posts_columns', 'add_acf_columns' );

This filter adds your additional columns to the list. We have created an array containing two items – one for the start date and one for the end date – and merged it with the existing columns. The filter is hooked to the specific post type, in this case, manage_hosting_posts_columns, based on the format manage_POSTTYPE_posts_columns. You’ll need to edit this filter to match your custom post type slug.

Secondly, add the following code to output the meta field values:-


 /*
 * Add columns to Hosting CPT
 */
 function hosting_custom_column ( $column, $post_id ) {
   switch ( $column ) {
     case 'start_date':
       echo get_post_meta ( $post_id, 'hosting_start_date', true );
       break;
     case 'end_date':
       echo get_post_meta ( $post_id, 'hosting_end_date', true );
       break;
   }
}
add_action ( 'manage_hosting_posts_custom_column', 'hosting_custom_column', 10, 2 );

Again, notice how the action hook is specific to your post type, in this case, manage_hosting_posts_custom_column. The function looks for the name of your custom columns then echoes the metadata.

Awesome, we’ve added the fields now! But wait, do you want to go the extra step and make the fields sortable? Of course, why wouldn’t you! Here’s how we can do that:-


 /*
 * Add Sortable columns
 */

function my_column_register_sortable( $columns ) {
	$columns['start_date'] = 'start_date';
	$columns['end_date'] = 'start_date';
	return $columns;
}
add_filter('manage_edit-hosting_sortable_columns', 'my_column_register_sortable' );

Well, we hope you have found this tutorial usual, be sure to leave a comment if this has helped you or if you require any help!

Nathan da Silva - Profile

Posted by: Nathan da Silva

Nathan is the Founder of Silva Web Designs. He is passionate about web development, website design and basically anything digital related. His main expertise is with WordPress, Magento, Shopify as well of many other frameworks. Whether you need responsive design, SEO, speed optimisation or anything else in the world of digital then get in touch. If you would like to work with Nathan, simply drop him an email at [email protected]

It’s good to share