自定义帖子类型固定链接不适用于分层父级

时间:2019-01-29 04:22:12

标签: php wordpress custom-wordpress-pages

如果我为帖子设置“ post_parent”,那么我创建的帖子类型将无法使用。我收到404错误,如果该职位没有父母,则可以正常工作。这是我所做的:

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);
}

出什么问题了?谁能解释一个解决方案?

1 个答案:

答案 0 :(得分:0)

替换此

$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')
    );

与此

$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();

注意::添加flush_rewrite_rules(),刷新页面一次或两次,然后 立即删除它 。除非根据食典中的规定,否则您不应保留flush_rewrite_rules()