StreetCoder
StreetCoder

Reputation: 10061

How to change the URL of single post of custom post type in Wordpress

I have created the custom post type guides in wordpress. So, I can see all the posts of guides under the URL: http://example.com/guides/. When I click to see the detail view of any post of guides, the URL structure has been http://example.com/guides/any-post-of-guide/. Here I just want: for all guides, the url should have guides and for the single post of guide, the url structure should have guide (instead of guides). Can you tell me someone how to do this.

The code below what I have done and tried:

$args = array(
    'labels'             => $labels,
    'description'        => __( 'Description.', 'your-plugin-textdomain' ),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => 'ml-vault.php',
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'guides'),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title','editor', 'excerpt', 'thumbnail')
);

Upvotes: 2

Views: 2129

Answers (2)

James Vu
James Vu

Reputation: 2471

  1. Change your rewrite argument to:
'rewrite' => array('slug' => 'guide', 'with_front' => false),
  1. Add rewrite rule for archive link:
add_action('init', function()
{
    add_rewrite_rule('^guides/?$', 'index.php?post_type=guides', 'top');
});
  1. Flush your permalink structure and now it should work.

Upvotes: 1

TheHelper
TheHelper

Reputation: 1

Have you reconsidered the argument rewrite in your args array? More about rewrite here - https://codex.wordpress.org/Function_Reference/register_post_type#rewrite

Upvotes: 0

Related Questions