douchui4459 2019-01-29 04:22
浏览 253

自定义发布类型永久链接不适用于分层父级

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?

  • 写回答

1条回答 默认 最新

  • dsx58940 2019-01-29 09:54
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)