Search code examples
wordpresswordpress-themingcustom-post-typecustom-wordpress-pagescustom-taxonomy

Custom wordpress post type with parents


First of all, this is a very beginner type of question so I hope you understand my idiocy.

I would like to create a WordPress theme similar to https://www.novelfull.com where there is a parent post(which is a book post) with all the child posts (the chapters of that book) in it. It is possible to make a WordPress category to book post and the WordPress post as its chapter posts, but I would like to have something more dedicated, like a custom post type that accepts children posts or something similar if it exists, (as long as it adds children posts(chapters) to parent post(books) automatically when I add them). Is there a feature like this in WordPress?


Solution

  • Yes, it is quite well documented here: https://developer.wordpress.org/reference/functions/register_post_type/

    To get you started, paste the following code in your functions.php or even better, create a custom plugin.

        function create_book() {
      register_post_type( 'book',
        array(
          'labels' => array(
            'name' => __( 'Books' ),
            'singular_name' => __( 'book' ),
            'add_new' => _x('Add book', 'book'),
            'add_new_item' => __('Add book'),
            'edit_item' => __('Edit book'),
            'new_item' => __('New book'),
            'view_item' => __('View book'),
            'search_items' => __('Search book'),
            'not_found_in_trash' => __('Niets gevonden in de prullenbak'),
          ),
          'public' => true,
          'menu_icon' => 'dashicons-book-alt',
          'rewrite' => array( 'slug' => 'book', 'with_front' => true ),
          'menu_position' => 3,
          'hierarchical' => true,
          'supports' => array(
                    'title',
                    'page-attributes',
                    'thumbnail',
                    'editor',
                    'excerpt',
                    'author',
                    'comments',
                    'custom-fields',
                ),
        )
      );
    }
    add_action( 'init', 'create_book' );