创建具有相同slug的自定义帖子类型和子页面

时间:2018-02-09 14:14:13

标签: wordpress custom-wordpress-pages

我创建了一个自定义帖子类型,其中'has_archive'为'false',以便能够创建一个与自定义帖子类型相同的slug页面:'Test'。

我想为“测试”页面创建子页面/子页面,但是我收到错误404页面。

有一种方法可以优先处理自定义帖子类型的网页吗?

add_action( 'init', 'cptui_register_my_cpts_test' );
function cptui_register_my_cpts_test() {
  $labels = array(
    "name" => __( 'Test', 'test' ),
    "singular_name" => __( 'Test', 'test' ),
    );

  $args = array(
    "label" => __( 'Test', 'test' ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "has_archive" => false,
    "show_in_menu" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => true,
    "rewrite" => array( 'slug' => 'test'),
    "query_var" => true,
    "supports" => array( "title", "editor", "thumbnail" ),
      );
  register_post_type( "test", $args );
}

1 个答案:

答案 0 :(得分:1)

function custom_rada_type() {
        $labels = array(
            'name'                => 'Porady',
            'singular_name'       => 'Porady',
            'menu_name'           => 'Porady',
            'parent_item_colon'   => 'Kategoria rodzica',
            'all_items'           => 'Wszystkie porady',
            'view_item'           => 'Wyświetl poradę',
            'add_new_item'        => 'Dodaj poradę',
            'add_new'             => 'Dodaj poradę',
            'edit_item'           => 'Edytuj poradę',
            'update_item'         => 'Zaktualizuj poradę',
            'search_items'        => 'Wyszukaj poradę',
            'not_found'           => 'Nie znaleziono',
            'not_found_in_trash'  => 'Nie znaleziono w koszyku',
        );

        $args = array(
            'label'               => 'rada',
            'description'         => 'Szablony porad',
            'labels'              => $labels,
            'supports'            => array( 'title', 'editor', 'thumbnail', 'comments' ),
            'taxonomies'          => array( '' ),
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'can_export'          => true,
            'has_archive'         => 'porady',
            'rewrite' => array( 'slug' => 'porady', 'with_front' => false ),
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'capability_type'     => 'post',
            'menu_icon'           => 'dashicons-editor-help',
        );

        register_post_type( 'rada', $args );

    }
    add_action( 'init', 'custom_rada_type', 0 );
  1. 创建自定义帖子类型
  2. 使用slug创建页面 - porady
  3. 添加重写规则
  4. add_action('init', function () { add_rewrite_rule('porady/?$','index.php?pagename=porady', 'top'); flush_rewrite_rules(); }, 1000);

    1. 更改后,在"设置>完全刷新你永久链接固定链接" 你将收到 - site.com/porady(page with slug porady)和site.com/porady/article(custom post article)