How to do it...

Follow these steps to create a custom archive page for the book post type in order to understand the use of WordPress Loop:

  1. Open the WPCookbook Chapter 7 plugin using the file explorer.
  2. Create a new file called archive_books.php inside the WPCookbook Chapter 7 plugin.
  3. Add the following code block for the archive template:
<?php
get_header();
?>

<section id="primary" class="content-area">
<main id="main" class="site-main">
<!-- Step 4 code should be placed in the next line -->

</main><!-- #main -->
</section><!-- #primary -->

<?php
get_footer();
  1. Add the following code clock within the opening and closing <main> tags to display books details:
<?php 
if ( have_posts() ) : ?>
<header class="page-header" style="text-align: center;
font-size: 60px;">
<?php echo __('Book List','wpccp_ch7'); ?>
</header>
<?php
while ( have_posts() ) : the_post();
?>
<div style="margin:30px auto;width:80%;
background: #afc8e2;padding: 10px">
<div style="text-align: center;text-align: center;
font-size: 25px;font-weight: bold;" ><?php the_title();
?></div>
<div><?php the_content(); ?></div>
</div>

<?php
endwhile;

endif;
?>
<?php get_template_part( 'template-parts/pagination' ); ?>
  1. Add the following code to the end of the wpcookbookchapter7.php file to use the custom archive_books.php template for the book archive page:
add_filter('template_include', 'wpccp_chapter7_book_list_template');
function wpccp_chapter7_book_list_template( $template ) {
if ( is_post_type_archive('book') ) {
return plugin_dir_path(__FILE__) . 'archive_books.php';
}
return $template;
}
  1. Save the changes to file.

Now, go to the frontend of the site and access the book list. The book list can be accessed by using the http://www.yoursite.com/book/ URL. Replace www.yoursite.com with the URL of your site. You will see a customized book list, as shown in the following screenshot:

The custom design contains a title and content with a custom background color, instead of the default post design.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset