简体   繁体   中英

Custom Post Type permalink don't work with hierarchical parents

The post type, which I've created is not working if I set "post_parent" for the post. I get 404 error, if the post hasn't got a parent, it works fine. Here's what I did:

add_action( 'init', 'initPostType' );
function initPostType() {
    $posts_label = array(
        'name'                  => 'Insurance Posts',
        'singular_name'         => 'Insurance Post',
        'add_new'               => 'Add Insurance Post',
        'add_new_item'          => 'Add New Post',
        'edit_item'             => 'Edit Post',
        'new_item'              => 'New Post',
        'all_items'             => 'All Posts',
        'view_item'             => 'View Posts',
        'search_items'          => 'Search Posts',
        'not_found'             => 'No Posts found',
        'not_found_in_trash'    => 'No Posts found in Trash',
        'parent_item_colon'     => '',
        'menu_name'             => 'Insurance Posts'
    );
     $posts_args = array(
        'labels'                => $posts_label,
        'public'                => true,
        'show_in_menu'          => true,
        'has_archive'           => false,
        'hierarchical'          => true,
        'supports'              => array('title', 'editor', 'author', 'thumbnail', 'excerpt','page-attributes'),
        'taxonomies'            => array('category')
    );
    register_post_type('insurance_post', $posts_args);
}

add_action( 'add_meta_boxes', 'initPostsMetaBox' );
add_action( 'init', 'changePermaLinks' );

function initPostsMetaBox() {
    add_meta_box( 'post_parent', 'Parent', 'postParentMetaBox', 'insurance_post', 'side', 'high' );
}

function postParentMetaBox($post) {
    $post_type_object = get_post_type_object( $post->post_type );
    $pages = wp_dropdown_pages(array('post_type' => 'page', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __( '(no parent)' ), 'sort_column' => 'menu_order, post_title', 'echo' => 0 ));
    if ( !empty( $pages ) ) echo $pages;
}

function changePermaLinks() {
    add_permastruct('insurance_post', '/%insurance_post%', false);
}

What's the problem? Can anyone explain a solution for this?

Replace this

$posts_args = array(
        'labels'                => $posts_label,
        'public'                => true,
        'show_in_menu'          => true,
        'has_archive'           => false,
        'hierarchical'          => true,
        'supports'              => array('title', 'editor', 'author', 'thumbnail', 'excerpt','page-attributes'),
        'taxonomies'            => array('category')
    );

with this

$posts_args = array(
        'labels'                => $posts_label,
        'public'                => true,
        'show_in_menu'          => true,
        'has_archive'           => false,
        'hierarchical'          => true,
        'supports'              => array('title', 'editor', 'author', 'thumbnail', 'excerpt','page-attributes'),
        'taxonomies'            => array('category'),
        'rewrite'               => array( 'slug' => 'insurance_post' ) //choose your name
    );
flush_rewrite_rules();

TAKE NOTE: Add flush_rewrite_rules() , refresh the page once or twice and REMOVE IT IMMEDIATELY . You SHOULD NOT keep flush_rewrite_rules() unless under the provisions as in the codex.

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