简体   繁体   中英

Using a Wordpress Page as Base for Custom Post Type Permalinks

So I'm trying to setup a website in Wordpress with the following permalink structure:

http://www.site.com/trades/trade-type

I would like trades to be an actual page, and I have created trade-type as a custom post type.

In my trade-type custom post type, I set the slug to be 'trades' to use that permalink as my base structure. So all posts I create under the custom post type should proceed the /trades URL.

When I set this slug, I am able to successfully save and view pages on the /trades/trade-type pages. But when I try to go to the /trades page, it craps out.

When I change the slug to 'trades2' in my custom post type, I am able to successfully view the /trades page, however all trade-types I create are now under trades2/trade-type.

There seems like there's some sort of conflict between the /trades page and the custom post type slug. Is there a way I can edit functions.php to accommodate both? Any help would be appreciated.

Below is my functions.php for the trades-type custom post type

//Register Custom Trades Post Type
function custom_trades() {

$labels = array(
    'name'                => _x( 'Trades', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Trade', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Trades', 'text_domain' ),
    'parent_item_colon'   => __( 'Parent Trade:', 'text_domain' ),
    'all_items'           => __( 'All Trades', 'text_domain' ),
    'view_item'           => __( 'View Trade', 'text_domain' ),
    'add_new_item'        => __( 'Add New Trade', 'text_domain' ),
    'add_new'             => __( 'Add New', 'text_domain' ),
    'edit_item'           => __( 'Edit Trade', 'text_domain' ),
    'update_item'         => __( 'Update Trade', 'text_domain' ),
    'search_items'        => __( 'Search Trade', 'text_domain' ),
    'not_found'           => __( 'Not found', 'text_domain' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
);
$rewrite = array(
    'slug'                => 'trades',
    'with_front'          => true,
    'pages'               => true,
    'feeds'               => true,
);
$args = array(
    'label'               => __( 'ds_trades', 'text_domain' ),
    'description'         => __( 'Post Type Description', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'page-attributes', ),
    'taxonomies'          => array( 'category' ),
    'hierarchical'        => true,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'menu_icon'           => 'dashicons-admin-generic',
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'rewrite'             => $rewrite,
    'capability_type'     => 'page',
);
register_post_type( 'ds_trades', $args );

}

// Hook into the 'init' action
add_action( 'init', 'custom_trades', 0 );

You are asking why it freaks out with domain.com/trades but pages with domain.com/trades/trade-article. are working.

I'm sure WordPress is searching for an archive in this moment which is called archive-trades.php inside your theme folder.

detailed instructions found here http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-post-types-archive-page-in-wordpress/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM