full guide for creating a “Book” Custom Post Type (CPT) in WordPress

1. Register Book Custom Post Type

File: functions.php of your child theme (hello-elementor-child/functions.php)

<?php
// Register Book CPT
function hello_register_book_cpt() {
$labels = array(
‘name’ => _x(‘Books’, ‘Post Type General Name’, ‘hello’),
‘singular_name’ => _x(‘Book’, ‘Post Type Singular Name’, ‘hello’),
‘menu_name’ => __(‘Books’, ‘hello’),
‘name_admin_bar’ => __(‘Book’, ‘hello’),
‘add_new_item’ => __(‘Add New Book’, ‘hello’),
‘edit_item’ => __(‘Edit Book’, ‘hello’),
‘view_item’ => __(‘View Book’, ‘hello’),
‘all_items’ => __(‘All Books’, ‘hello’),
‘search_items’ => __(‘Search Books’, ‘hello’),
);

$args = array(
‘label’ => __(‘Book’, ‘hello’),
‘description’ => __(‘Books and Reviews’, ‘hello’),
‘labels’ => $labels,
‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’, ‘excerpt’, ‘comments’),
‘taxonomies’ => array(‘category’, ‘post_tag’), // Enable default categories
‘hierarchical’ => false,
‘public’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘menu_position’ => 5,
‘menu_icon’ => ‘dashicons-book’,
‘show_in_admin_bar’ => true,
‘show_in_nav_menus’ => true,
‘can_export’ => true,
‘has_archive’ => true,
‘rewrite’ => array(‘slug’ => ‘books’),
‘show_in_rest’ => true, // Gutenberg support
);

register_post_type(‘book’, $args);
}
add_action(‘init’, ‘hello_register_book_cpt’, 0);

✅ This creates a Book CPT with categories and tags enabled.


Create Frontend Page Template

If you want to show books in a custom layout using WP_Query:

File: template-books.php in your child theme (hello-elementor-child/template-books.php)

<?php
/**
* Template Name: Books Page
*/

get_header(); ?>

<div class=”books-page”>
<h1>All Books</h1>

<?php
// WP_Query to get all books with category
$args = array(
‘post_type’ => ‘book’,
‘posts_per_page’ => -1, // Show all books
‘orderby’ => ‘date’,
‘order’ => ‘DESC’,
);

$books_query = new WP_Query($args);

if ($books_query->have_posts()) :
echo ‘<div class=”books-grid”>’;
while ($books_query->have_posts()) : $books_query->the_post(); ?>
<div class=”book-card”>
<?php if ( has_post_thumbnail() ) : ?>
<a href=”<?php the_permalink(); ?>”>
<?php the_post_thumbnail(‘medium’); ?>
</a>
<?php endif; ?>
<h2><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h2>
<p><?php echo wp_trim_words(get_the_excerpt(), 20); ?></p>
<p>Category: <?php the_category(‘, ‘); ?></p>
</div>
<?php endwhile;
echo ‘</div>’;
wp_reset_postdata();
else :
echo ‘<p>No books found.</p>’;
endif;
?>
</div>

<?php get_footer(); ?>


3.

Assign the Template to a Page

  1. In WordPress admin → Pages → Add New → “All Books” (or any title).

  2. On the right sidebar Page Attributes → Template, select Books Page.

  3. Publish the page → visit it to see your book list.


Add Custom CSS

Since you already have custom/custom.css, add this for styling:

File: custom/custom.css

.books-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}

.book-card {
border: 1px solid #ddd;
padding: 15px;
border-radius: 8px;
transition: box-shadow 0.3s ease;
background-color: #fff;
}

.book-card:hover {
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.book-card img {
max-width: 100%;
height: auto;
border-radius: 5px;
}

.book-card h2 {
margin: 10px 0 5px;
font-size: 1.2em;
}

.book-card p {
margin: 5px 0;
font-size: 0.95em;
color: #555;
}


Make sure your child theme’s functions.php enqueues this CSS:

function hello_enqueue_custom_styles() {
wp_enqueue_style(
‘custom-style’,
get_stylesheet_directory_uri() . ‘/custom/custom.css’,
array(),
filemtime(get_stylesheet_directory() . ‘/custom/custom.css’)
);
}
add_action(‘wp_enqueue_scripts’, ‘hello_enqueue_custom_styles’);


Optional: Display Books by Category Only

You can modify the WP_Query in template-books.php to show a specific category:

$args = array(
‘post_type’ => ‘book’,
‘posts_per_page’ => -1,
‘orderby’ => ‘date’,
‘order’ => ‘DESC’,
‘category_name’ => ‘fiction’, // Replace with your category slug
);

Share on Facebook Share on Twitter