Displaying Posts of a Custom Type

I currently have a new custom post type– “Books.” I have a custom rewrite url that I set when registering my post type:

'rewrite' => array( 'slug' => 'books', 'with_front' => false )

It works great when I visit a specific post, but how do I take control of the /books url for displaying a list of ALL of the books by date posted?

Related posts

Leave a Reply

1 comment

  1. Once way of doing it – I am sure there are other ways too – is to make a page template for books: books.php
    You can make a copy of page.php and rename it.
    In the top of the file you place the usual Page Template code as in:

    <?php
    /* 
    Template Name: Books
    */
    get_header(); ?>
    

    Below the normal loop, you then place the code for a new loop:

    <?php $loop = new WP_Query( array( 'post_type' => 'books') ); ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    

    Don’t forget to close it with the call for <?php endwhile; ?> at the end.

    With this method you can within WordPress add a Page called Books and assign this particular Template to it. This way you can still add general text to the page too and below display the list of all books.