5

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?

1
  • archive-cpt.php where cpt is the name of your custom post type Jun 17, 2015 at 17:11

2 Answers 2

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.

0

I would...

  1. Create a CPT registered with 'has_archive' => false and use that as your "editable content" source.
  2. Or use a simple "Page" if you prefer
  3. Then create archive-{cpt-name}.php in the theme and query in that template for the content CPT data, or for the "Page" data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.