I always run into an issue with creating archive or landing pages for custom post types and wanted to know how others handle this.

If I want to have a landing page for a custom post type, however I want the archive page to contain editable content (i.e. be a physical page). I will need to make a custom template for my archive page. This issue with this is that there are no relationship of this page to the custom post type. So now I need to set archive to false and the rewrite rule to match the slug of the custom post type. Also when viewing a custom post type single page, the menu doesn’t know the custom landing page is active.

Does anyone have a logical solution for this?

2 Answers
2

For this situation, I create a custom page template, say tpl-archive-page.php.
For example using Advanced Custom Fields and the Post Type Selector Field(*) the user can choose a post type to connect to.

<?php
/**
 * Template Name: CPT Archive Page
 */

get_header();

while (have_posts()) :

    the_post();

    get_template_part('content', 'page');

    $archive_query_post_type = get_post_meta(get_queried_object_id(), 'cpt_archive_query_post_type', true);

    if( $archive_query_post_type && is_string($archive_query_post_type) ) {
        $args = array(
            'post_type' => $archive_query_post_type,
            'posts_per_page'=> -1,
            'orderby'       => 'title',
        );

        $archive_query = new WP_Query( $args );

        if ( $archive_query->have_posts() ) {
            while ( $archive_query->have_posts() ) {
                $archive_query->the_post();
                get_template_part('content', get_post_type() );
            }
        }

        wp_reset_postdata();
    }

endwhile;

get_footer();

The has_archive option when registering the custom post type must be set to false.


(*) If you are using ACF5/Pro, consider this fork of the field.

Leave a Reply

Your email address will not be published. Required fields are marked *