I have a workflow / best practice question about making views for Custom Post Types (both list and detail).
Currently, I put the “list” view for my Custom Post Type (called, “Menu Items”) into a WordPress Page called “Menu”. I then use a custom template for my Menu “Page”:
<?php
$type = 'menu_item'; // name of my custom post
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 5,
'ignore_sticky_posts'=> 1
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
// my custom menu item post meta stuff
<?php endwhile; ?>
<?php endif; ?>
I put my “detail” view into a PHP page called “single-menu_item.php”.
On the front-facing side, this works well. However, I was just wondering if there is another way to do this because I see a few non-intuitive things about this:
-
My user goes to a top-level menu item called “Menu Items” but then the content is displayed on a WordPress Page (which they basically don’t touch). Do I just “hide” this page from the user using permissions, and if so, can someone point me in the right direction as to how to go about doing this??
-
Sometimes on the site I just need to show a list view for the custom post but not the “detail” view…so I haven’t built a single view page for that particular custom post (say I make another custom post type called something other than “menu item”). So…when the user hits “view”, because I haven’t make a proper single view for that post, they get bumped to the default view, which might not be properly formatted at all?
I’m interesting in knowing how other people go about dealing with these issues (or if they bother with it at all)…
For the main view of items in a custom post type you don’t need to use a page template at all.
Create a file called
archive-post_type_name.php
containing the loop for that post type. When you register the post type set thehas_archive
value to the URL slug you want the posts to appear on eg. ‘menu’ then browse to http://yoursite.com/menu/ to see the post type archive.If you don’t want people going to the post type page on its own the simplest way is to just leave out the permalinks in your template.