WordPress, custom post type relationship to many post

I have an e-book that I would like to integrate in wordpress.
I would have to get a page listing the books from a custom post “books”.
When clicking on a book, I would display the different chapter of the book.
Finally when clicking on a chapter i would like to display it.
Knowing that chapters would be store in a custom post “chapters”.
I made some search for custom type relationship one to many.
Help welcome knowing that if you have another solution to offer me rather than go through the custom types I’m interested too.

Related posts

2 comments

  1. I think you are overthinking this, there are several ways to achieve what you want without any custom post types…

    Categories are hierarchical (as opposed to tags) so:

    • your book can be a top level category (or you can have a top level category called “books” under which you place all books)
    • your chapters are simply posts under each of the categories
    • or you could make chapters into categories, and have each individual page be a post under that

    You don’t need any custom post types to do this, though it might be nice to separate the books from other posts by using one custom post type for chapters (or individual pages if you go that way) and using one custom taxonomy for the books (and chapters if you split to individual pages).

    You could even do it using only the parent / child relationship of standard wordpress’ pages (they are hierarchical too!):

    • A book is a page
    • Under each book is a child page that holds the chapter text (just create a new page and set its parent to the books main page)

    Now to actually display everything the way you want you will probably want to study the template hierarchy and create some custom templates for your specific “types”

  2. I think best way is custom post type i.e book but if don’t want it then you can create a category for that i.e book and for chapter you can create sub categories like chapter1 and chapter2 and so on…

    And follow below code for displaying list of all subcategories….

     <ul>
          <?php
    $categories = wp_list_categories('echo=0&title_li=0&child_of=2');
    $categories = preg_replace('/title="(.*?)"/','',$categories);
    echo $categories;
    ?>
        </ul>
    

Comments are closed.