Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I still don't quite understand when it's best to use custom post types vs custom taxonomies.. or both together at once. I have Menus as my custom post type. There's nothing about it that needs to look different from any other post on the site though. What I need to do is display a list of Types of menus (traditional, Indian, Kosher etc), and then below each type, a list of each post(menu) of that type. How is the best way to do this? I'd like to keep it in a separate section from the blog, and I love the custom menu item in the admin, so it's ideal that way. I'm just stuck on how to get essentially a "category" page (menus) that would list all the subcategories (types of menus) and all posts in those subcategories.

share|improve this question
    
Did my answer help you out? If so please accept it. –  Ben Sep 5 '12 at 22:24
    
this was excellent, thank you! –  jamie Sep 11 '12 at 23:54

1 Answer 1

up vote 1 down vote accepted

I like to think of a custom post type as how you define the structure of your content, so menus is a perfect example of how to use a custom post type to structure your post data to be more like a menu.

Your need to categorize your menus into different types would be well met by a custom taxonomy. For how to do this, please see the codex entry for register_taxonomy()

Once you have your menu type custom taxonomy setup, and some menus assigned to the different menu types, it will be time to build a template file that presents the data as you wanted. I'm not sure this will be the best way, but it should work pretty well. You can start a new(or duplicate) template file, and call it archive-menus.php (presuming "menus" is the system name for the custom post type you made).

Now, in the main content area of your new archive template, copy and paste the following code that will get all terms from your custom taxonomy, and loop through them, displaying a list of menus under each taxonomy term(like a category).

<?php 
// change 'taxonomyName' to the system name of your custom taxonomy.
$terms = get_terms('taxonomyName', array(
    'orderby' => 'name',
    'order' => 'ASC'
));
echo "<ul id='menu-types'>";
foreach($terms as $term) {
    $termSlug = $term->slug;
    echo "<li><h2>".$term->name."</h2><ul>";
    query_posts('post_type=menus&taxonomyName='.$termSlug);
    while ( have_posts() ) : the_post(); 

                 echo "<li>".get_the_title()."</li>"; 

    endwhile;
    echo "</ul></li>";
}
echo "</ul>";
 ?>

Presuming your custom taxonomy is set up properly, and both instances of 'taxonomyName' in the ^^^above code were edited to reflect your custom taxonomy name, then the loop should display the data you need. Using "pretty" permalinks you should be able to link to the archive-menus.php template with a url like http://yoursite.com/menus. Just be sure that where you register_post_type() in your functions.php file, you have set the 'has_archive' => true, option.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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