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
版权声明:本文标题:build Custom post type and make it child for Posts and put it as comment 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741509481a2382522.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论