1

Have Wordpress in the root of the site. Want the URL structure for pages to be example.com/{page-slug} and for posts to be example.com/academy/{post-slug}. I have my permalinks set to custom with /academy/%postname% as the structure.

This works great. However, I have a custom post type called definitions. I would like the definitions URL structure to be example.com/glossary/{definition-slug}. Currently with the above set up I am getting a URL of example.com/academy/definition/{definition-slug}

Is there a way to exclude custom post types from the post permalink settings?

Here is my custom post definition:

function create_posttypes() {
    $labels1 = array(
            'name' => __( 'Definitions' ),
            'singular_name' => __( 'Definition' ),
            'all_items' => __( 'All Definitions' ),
            'view_item' => __( 'View Definition' ),
            'add_new_item' => __( 'Add New Definition' ),
            'edit_item' => __( 'Edit Definition' ),
            'update_item' => __( 'Update Definition' ),
            'search_items' => __( 'Search Definitions' ),
            'not_found' => __( 'Not Found' ),
            'not_found_in_trash' => __( 'Not Found in Trash' ),
        );
    $args1 = array(
            'label' => 'Definitions',
            'description' => 'Definitions received',
            'labels' => $labels1,
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug', 'definitions'),
            'hierarchical' => false,
            'show_ui' => true,
            'show_in_menu' => true,
            'show_in_nav_menus' => true,
            'show_in_admin_bar' => true,
            'menu_position' => 5,
            'can_export' => true,
            'exclude_from_search' => false,
            'publicly_queryable' => true,
            'capability_type' => 'page',
            'supports' => array('title','thumbnail', 'revisions', 'page-attributes')
        );
    register_post_type( 'definitions', $args1 );
}
  • 1
    for the rewrite args try this : ["slug" => "glossary", "with_front" => FALSE] – mmm Sep 4 '16 at 17:10
1

You can use the rewrite parameter to disable the front portion of the URLs (/academy/ in your case) for custom post types.

function create_posttypes() {
    $labels1 = array(
            'name' => __( 'Definitions' ),
            'singular_name' => __( 'Definition' ),
            'all_items' => __( 'All Definitions' ),
            'view_item' => __( 'View Definition' ),
            'add_new_item' => __( 'Add New Definition' ),
            'edit_item' => __( 'Edit Definition' ),
            'update_item' => __( 'Update Definition' ),
            'search_items' => __( 'Search Definitions' ),
            'not_found' => __( 'Not Found' ),
            'not_found_in_trash' => __( 'Not Found in Trash' ),
        );
    $args1 = array(
            'label' => 'Definitions',
            'description' => 'Definitions received',
            'labels' => $labels1,
            'public' => true,
            'has_archive' => true,
            'rewrite' => array(
                'slug' => 'glossary',
                'with_front' => false,
             ),
            'hierarchical' => false,
            'show_ui' => true,
            'show_in_menu' => true,
            'show_in_nav_menus' => true,
            'show_in_admin_bar' => true,
            'menu_position' => 5,
            'can_export' => true,
            'exclude_from_search' => false,
            'publicly_queryable' => true,
            'capability_type' => 'page',
            'supports' => array('title','thumbnail', 'revisions', 'page-attributes')
        );
    register_post_type( 'definitions', $args1 );
}
  • Thanks! Removed the academy. However I changed the slug from definitions to glossary and it is still showing up with URL of example.com/definitions/{def-slug} instead of example.com/glossary/{def-slug} – jppower175 Sep 4 '16 at 18:14
  • 1
    Head over to the permalinks page in the admin. That will reload the permalinks and should fix that issue. (Merely visiting the permalinks page should be enough, but I tend to click the save button anyway.) – Dave Romsey Sep 4 '16 at 18:18
  • Tried re-saving on permalinks page... no luck – jppower175 Sep 4 '16 at 18:31
  • 1
    Hrmm, I tested the code that I posted and verified that it's working (updated the slug to glossary, for the record. My bad on missing that). The fact that definitions is still appearing in your URL suggests that the permalinks are not being flushed. – Dave Romsey Sep 4 '16 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.