0

I have used the CptUI plugin to create a 'Products' CPT. The admin side of things seems to work fine but I can't quite get the front end to display how I'd like.

For example I create a product, say "Guitars" and another called "Drums". Their permalinks in admin are dispay as http://mysite.com/products/guitars and http://mysite.com/products/drums respectively which looks great. However, when I navigate to one of the URLS, I see a page that lists all the current products. This makes sense considering my single-products.php contains the following:

<?php
 /*Template Name: Products Template
 */

get_header(); ?>
<div id="primary">
    <div id="content" role="main">
    <?php
    $mypost = array( 'post_type' => 'products', );
    $loop = new WP_Query( $mypost );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <header class="entry-header">

                <div style="float: right; margin: 10px">
                    <?php the_post_thumbnail( array( 100, 100 ) ); ?>
                </div>

                <strong>Name: </strong><?php the_title(); ?><br />

            </header>

            <div class="entry-content"><?php the_content(); ?></div>
        </article>

    <?php endwhile; ?>
    </div>
</div>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>

What I'd ideally like is for http://mysite.com/products/guitars to display only the single CPT with the guitars content, (the same for the drums) and then http://mysite.com/products to display all the products (As it currently does for http://mysite.com/products/guitars). Is this possible and if so, how?

1 Answer 1

1

You must ensure that the post type is registered with has_archive property set to true

Look at codex example, replace 'Books' with 'Products'

function codex_custom_init() {
  $labels = array(
    'name' => 'Books', 'singular_name' => 'Book',
    'add_new' => 'Add New', 'add_new_item' => 'Add New Book',
    'edit_item' => 'Edit Book', 'new_item' => 'New Book',
    'all_items' => 'All Books', 'view_item' => 'View Book',
    'search_items' => 'Search Books', 'not_found' =>  'No books found',
    'not_found_in_trash' => 'No books found in Trash', 'parent_item_colon' => '',
    'menu_name' => 'Books'
  );

  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => array( 'slug' => 'book' ),
    'capability_type' => 'post',
    'has_archive' => true, // <- this arg must be true
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  ); 
  register_post_type( 'book', $args );
}

add_action( 'init', 'codex_custom_init' );

If you are sure 'has_archive' is true, in your theme root create a file named products.php that should be someting like:

<?php get_header(); ?>
<div id="primary">
    <div id="content" role="main">
    <?php while ( have_posts() ) : the_post(); ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <header class="entry-header">
                <div style="float: right; margin: 10px">
                    <?php the_post_thumbnail( array( 100, 100 ) ); ?>
                </div>
                <strong>Name: </strong><?php the_title(); ?><br />
            </header>
            <div class="entry-content"><?php the_content(); ?></div>
        </article>
    <?php endwhile; ?>
    </div>
</div>
<?php get_footer(); ?>

Than create a file named archive-products.php and put there simply:

get_template_part('products');

Than create a file named single-products.php and put there simply:

// yes, exactly the same thing
get_template_part('products');
1
  • Thanks that's great! That did exactly what I was after. It'd be helpful if the documentation made it more explicit that the has_archive flag was used for this.
    – Sonoman
    Aug 4, 2013 at 14:47

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.