自定义帖子类型无法访问子页面

时间:2015-10-25 作者:Maria Ozawa

我有以下自定义帖子类型,但它不起作用。我希望它使用以下URL运行:example.com/cartoon-series/post-parent/watch/

add_action( \'admin_menu\', function() {
    remove_meta_box( \'pageparentdiv\', \'episodes\', \'normal\' );
} );
add_action( \'add_meta_boxes\', function() {
    add_meta_box( \'episodes-parent\', \'Cartoon Series\', \'episodes_attributes_meta_box\',
        \'episodes\', \'side\', \'default\' );
} );

add_action( \'init\', function(){
    $labels = array(
        "name" => "Cartoon Series",
        "singular_name" => "Cartoon Series",
        "menu_name" => "Cartoon Series",
        "all_items" => "All Cartoon Series",
        "add_new" => "Add New",
        "add_new_item" => "Add New Cartoon Series",
        "edit" => "Edit",
        "edit_item" => "Edit Cartoon Series",
        "new_item" => "New Cartoon Series",
        "view" => "View",
        "view_item" => "View Cartoon Series",
        "search_items" => "Search Cartoon Series",
        "not_found" => "No Cartoon Series Found",
        "not_found_in_trash" => "No Cartoon Series Found in Trash",
        "parent" => "Parent Cartoon Series",
    );

    $args = array(
        "labels" => $labels,
         "description" => "",
        "public" => true,
        "show_ui" => true,
        "has_archive" => true,
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => array( "slug" => "cartoon-series", "with_front" => false ),
        "query_var" => true,
        "supports" => array( "title", "revisions", "thumbnail" )
    );

    register_post_type( "cartoon-series", $args );

    $labels = array(
        "name" => "Episodes",
        "singular_name" => "Episode",
    );

    $args = array(
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "show_ui" => true,
        "has_archive" => true,
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "episodes", "with_front" => false ),
        "query_var" => true,
        "supports" => array( "title", "revisions", "thumbnail" )
    );

    register_post_type( "episodes", $args );

} );

add_action( \'add_meta_boxes\', function() {
    add_meta_box( \'episodes-parent\', \'Cartoon Series\', \'episodes_attributes_meta_box\',
        \'episodes\', \'side\', \'default\' );
} );

function episodes_attributes_meta_box( $post ) {
    $pages = wp_dropdown_pages( array( 
        \'post_type\' => \'cartoon-series\', 
        \'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;
    } // end empty pages check
}

// 3. Rewrite rule - teach WordPress to parse the custom URL pattern
function pwatch_rewrite_rule() {
    add_rewrite_rule( \'^cartoon-series/([^/]+)/watch/?\',
        \'index.php?episodes=$matches[1]-watch\', 
        \'top\' );
}
add_action( \'init\', \'pwatch_rewrite_rule\' );

add_filter( \'post_type_link\', function( $post_link,$link ) {
    if ( \'episodes\' == get_post_type( $post ) ) {
        //Lets go to get the parent cartoon-series name
            $parent = get_post( $post->post_parent );
            $post_link = get_bloginfo( \'url\' ) . \'/cartoon-series/\' . 
                $parent->post_name . \'/watch/\';
    }
    return $post_link;
}, 1, 4 );

1 个回复
SO网友:shadylane

您需要在参数中的“supports”中添加“page attributes”,以注册自定义帖子类型。

相关推荐