Vediamo com’è possibile creare una pagina di archivio personalizzata per custom post type.
Per la pagina di archivio usiamo un template di pagina. All’interno creiamo un loop personalizzato che richiama i custom post type, nel nostro esempio il custom post type si chiama classified.
<?php
/**
 * Template Name: Classified
 */
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
?>
<?php get_header(); ?>
    <div id="container" class="row-inner">
        <div id="content">
         <?php
            if( ! is_paged() ) :
            while ( have_posts() ) : the_post(); ?>
                <?php if ( has_post_thumbnail() ) :?>
                    <div class="post-preview"><?php the_post_thumbnail('large') ?></div>
                <?php endif; ?>
                <article id="post-<?php the_ID(); ?>" <?php post_class( 'clearfix' ); ?> role="article">
                    <div class="entry-content clearfix">
                  <?php the_content(); ?>
                    </div>
                </article>
         <?php endwhile;
         endif;
         ?>
            <div class="archive-classified">
         <?php
         $classified_post = new WP_Query( array(
            'posts_per_page' => get_option( 'posts_per_page' ),
            'post_type'      => 'classified',
            'paged' => $paged
         ) );
         $max_pages = $classified_post->max_num_pages;
         $args = array(
            'base'               => get_pagenum_link() . '%_%',
            'format'             => 'page/%#%',
            'total'              => $max_pages,
            'current'            => $paged,
            'show_all'           => false,
            'end_size'           => 1,
            'mid_size'           => 2,
            'prev_next'          => true,
            'prev_text'          => __('« Annunci Nuovi'),
            'next_text'          => __('Annunci vecchi »'),
            'type'               => 'list',
            'add_args'           => false,
            'add_fragment'       => '',
            'before_page_number' => '',
            'after_page_number'  => ''
         );
         ?>
                <nav class="post-navigation" role="navigation">
                  <?php echo paginate_links($args ) ?>
                </nav>
         <?php if ( $classified_post->have_posts() ) :
            while ( $classified_post->have_posts() ) : $classified_post->the_post(); ?>
                    <article id="post-<?php the_ID(); ?>" <?php post_class('post-entry clearfix'); ?> role="article">
                  <?php mnky_post_links(); ?>
                        <header class="post-entry-header">
                            <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'care' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                           <?php the_title(); ?>
                                </a>
                            </h2>
                            <span class="meta-date"><?php echo get_the_date() ?></span>
                            <span class="meta-category"><?php the_category( ', ' ) ?></span>
                        </header><!-- .entry-header -->
                        <div class="entry-summary">
                            <a href="<?php the_permalink() ?>">Vedi annuncio</a>
                        </div>
                    </article>
            <?php endwhile;
         else :
            get_template_part( 'content', 'none' );
         endif;
         wp_reset_postdata();
         ?>
            <nav class="post-navigation" role="navigation">
              <?php echo paginate_links($args ) ?>
            </nav>
        </div>
        </div><!-- #content -->
    </div><!-- #container -->
<?php get_footer(); ?>
Quando definiamo i custom post type tramite la funzione register_post_type il valore has_archive deve essere impostato a true come nel codice di esempio qui sotto.
$args = array( 'label' => __( 'Classified', 'TEXTDOMAIN' ), 'description' => __( 'Classified post', 'TEXTDOMAIN' ), 'labels' => $labels, 'supports' => array( 'title', 'editor', 'thumbnail' ), 'taxonomies' => array( 'category' ), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'menu_icon' => 'dashicons-screenoptions', 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'rewrite' => $rewrite, 'capability_type' => 'page', 'show_in_rest' => false, ); register_post_type( 'classified', $args );
Ora basta creare un pagina, assegnare il template di pagina classified e pubblicare.