I was given a basic umbrella theme to work with and modify for individual needs. I have added a few custom post types. I am having trouble modifying the index.php page to work with my new CPTs.

For example, one CPT is called is a page called beach_clean. It's on a page called beachclean.php and made it a template page like so:

  Template Name: Beach Clean Page
get_header();  ?>

Normally I would set up a wp_query like this to retrieve the specific CPT info:

$args = array(
  'post_type' => 'beach_clean'
$the_query = new WP_Query( $args );

and then modify the loop like so:

<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

however I am unsure how to do it based on how the original code is written.

Can anyone help me see how to insert a query for my CPT into the way the code is written?

Please and thank you!

Here is the index.php page:

    // Start the Loop.
    $index = 1;
    $total_results = $wp_query->post_count;
    while ( have_posts() ) : the_post();
     if ($index == 1):
        get_template_part('template-parts/post', 'hero');
        if ($total_results > 1):
            <section class="panel">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12">
                        <div class="media-block-flex-wrap flex-top">

        <?php endif; ?>
     <?php else: ?>

          <?php if (has_post_thumbnail()) {
              get_template_part('template-parts/content', 'media_block_top');
          } else { get_template_part('template-parts/content', 'media_block_no_img');
            } ?>

     <?php endif; ?>

         if ($total_results > 1): ?>
                </div> <!-- Media Block -->
        </div> <!-- Row -->
        <div class="row">
            <div class="col-xs-12 center-containing-text">
                // Previous/next page navigation.
                the_posts_pagination( array(
                    'screen_reader_text' => " ",
                    'prev_text'          => __( 'Previous page', 'twentysixteen' ),
                    'next_text'          => __( 'Next page', 'twentysixteen' ),
                    'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( '', 'twentysixteen' ) . ' </span>',
                ) );
<?php endif; ?>
  • 3
    is there a reason you are creating a page and a new query to output your CPT rather than using the built in post type archive that WordPress automatically generates? – Milo Oct 14 '16 at 3:00

I would suggest first adding a new file called


to your themes root directory.

Then copy and paste all the code from that index file into it.

After, if you haven't done so already, add this argument, 'has_archive' => true, to the arguments array used in the register_post_type function and in the same array make sure rewrite isn't set to false.

Then go to the wordpress admin area Settings->Permanlinks and make sure Post name is selected and click Save (this will flush your permalinks in case the post type wasn't registered already, now it will be).

Finally, you will be able to access your new page here http://your_domain.com/beach_clean

If you want to change the slug to something other than beach_clean, add the rewrite argument in as noted above, and make the value of it is array('slug' => 'your-custom-slug')

  • 1
    Thanks, stims. This worked. First time with more than one CPT using the same code to display. I now better understand how Archive pages work( which I clearly didn't before), much appreciated. – Ernie Oct 15 '16 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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