Query custom post type in the loop

I have a custom post type running called services:

// Services
function services_cpt() {
  $labels = array(
    'name'               => _x( 'Services', 'post type general name' ),
    'singular_name'      => _x( 'Service', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'production' ),
    'add_new_item'       => __( 'Add New Service' ),
    'edit_item'          => __( 'Edit Service' ),
    'new_item'           => __( 'New Service' ),
    'all_items'          => __( 'All Services' ),
    'view_item'          => __( 'View Service' ),
    'search_items'       => __( 'Search Services' ),
    'not_found'          => __( 'No services found' ),
    'not_found_in_trash' => __( 'No services found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'Services'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our services and service specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail' ),
    'has_archive'   => true,
  );
  register_post_type( 'services', $args ); 
}
add_action( 'init', 'services_cpt' );

I would like to use a page template to display the posts instead of archive-services.php. I have built the following page template:

Read More
<?php
/*
Template Name: Services
*/
// Get header
get_header(); ?>

<div id="primary">
<?php $loop = new WP_Query( array( 'post_type' => 'services', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<section class="col third">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('medium'); ?></a>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
</section>

<!-- End loop -->
<?php endwhile; wp_reset_query(); ?>

<!-- End primary -->
</div>
<?php get_footer(); ?>

When I go to the page on the front-end, it’s displaying the default archive.php. I can’t figure out what I’m doing wrong. Any help is much appreciated 🙂

Related posts

1 comment

  1. You are probably getting a rewrite conflict with the has_archive argument here:

     $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our services and service specific data',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail' ),
        'has_archive'   => true,
      );
    

    Set has_archive to false if you do not want the automatically generated archive.

    I don’t really see the logic of not using the template system though.

Comments are closed.