admin管理员组

文章数量:1291041

I want to build custom post type under name "lesson"

I try tis code

 function my_register_cpt() {
    $labels = array(
        'name'                  => _x( 'Lessons', 'Post Type General Name', '1fix' ),
        'singular_name'         => _x( 'Lesson', 'Post Type Singular Name', '1fix' ),
        'menu_name'             => __( 'Lessons', '1fix' ),
        'name_admin_bar'        => __( 'Lessons', '1fix' )
    );
    $args = array(
        'label'                 => __( 'Lesson', '1fix' ),
        'labels'                => $labels,
        'hierarchical'          => false,
        'public'                => true
    );
    register_post_type( 'lesson', $args );
}
add_action( 'init', 'my_register_cpt' );

function my_add_meta_boxes() {
    add_meta_box( 'lesson-parent', 'post', 'lesson_attributes_meta_box', 'lesson', 'side', 'high' );
}
add_action( 'add_meta_boxes', 'my_add_meta_boxes' );

function lesson_attributes_meta_box( $post ) {
    $post_type_object = get_post_type_object( $post->post_type );
    $pages = wp_dropdown_pages( array( 'post_type' => 'post', '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 my_add_rewrite_rules() {
    add_rewrite_tag('%lesson%', '([^/]+)', 'lesson=');
    add_permastruct('lesson', '/lesson/%postname%/%lesson%', false);
    add_rewrite_rule('^lesson/([^/]+)/([^/]+)/?','index.php?lesson=$matches[2]','top');
}
add_action( 'init', 'my_add_rewrite_rules' );

function my_permalinks($permalink, $post, $leavename) {
    $post_id = $post->ID;
    if($post->post_type != 'lesson' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
        return $permalink;

    $parent = $post->post_parent;
    $parent_post = get_post( $parent );

    $permalink = str_replace('%postname%', $parent_post->post_name, $permalink);

    return $permalink;
}
add_filter('post_type_link', 'my_permalinks', 10, 3);

but when I add new lesson, I can't choose Parent post

is there ant bug in this code?

and is there possibilities to put my custom post type as comments?

本文标签: build Custom post type and make it child for Posts and put it as comment