Search code examples
wordpressurl-rewritingpermalinks

Include parent page slug in permalink for posts


I have created a page called Blog and set it as the blog posts page from the wordpress settings.when I create the posts and publish the url structure is like

mysite.com/postname

i would like it to be like

mysite.com/blog/postname

so i edited the custom permalink structure to

mysite.com/blog/%postname%/

which is working.But i have another custom post type called Work now the permlink structure for work goes like

mysite.com/blog/work/workname

it was

mysite.com/work/workname

before i edited the permalink custom structure.

Is there any help to make it the way i want.ie..

mysite.com/blog/postname

&

 mysite.com/work/workname

Please help Thanks!!

Edit

my website is

http://jointviews.com/blog/work/best-school-bus-tracking-system/

http://jointviews.com/blog/building-long-term-relationships-with-customers-using-digital-media/

i have register the post type as follows

function work_register() {   

$labels = array( 
    'name' => _x('Work', 'post type general name'), 
    'singular_name' => _x('Work Item', 'post type singular name'), 
    'add_new' => _x('Add New', 'work item'), 
    'add_new_item' => __('Add New Work Item'), 
    'edit_item' => __('Edit Work Item'), 
    'new_item' => __('New Work Item'), 
    'view_item' => __('View Work Item'), 
    'search_items' => __('Search Work'), 
    'not_found' => __('Nothing found'), 
    'not_found_in_trash' => __('Nothing found in Trash'), 
    'parent_item_colon' => '' 
);   

$args = array( 
    'labels' => $labels, 
    'public' => true, 
    'publicly_queryable' => true, 
    'show_ui' => true, 
    'query_var' => true, 
    'menu_icon' => get_stylesheet_directory_uri() . '/article16.png', 
    'rewrite' => true, 'capability_type' => 'post', 
    'hierarchical' => true, 
    'menu_position' => null, 
    'supports' => array('title','editor','thumbnail') 
);   

register_post_type( 'work' , $args ); 

//register_taxonomy("categories", array("work"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => true));



}

Solution

  • You started perfectly and you are not far from a working solution, all you have to do is to change a little bit the way you register your "work" CPT, you need to change rewrite like this:

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

    the way it works is: if your permalink structure is /blog/, then your links will be: if with_front = false permalink: /news/ if with_front = true permalink: /blog/news/

    and the issue is that it defaults to true

    the wordpress codex page that has this info is here

    what you also have to note is: If registering a post type inside of a plugin, call flush_rewrite_rules() in your activation and deactivation hook (see Flushing Rewrite on Activation below). If flush_rewrite_rules() is not used, then you will have to manually go to Settings > Permalinks and refresh your permalink structure before your custom post type will show the correct structure.