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've been searching Google for this to no avail.

I'm working on a restaurant site, and I have a custom post type for dishes, like so:

$args = array(
    'labels'=> $labels,
    'public'=> true,
    'publicly_queryable'=>true,
    'show_ui'=>true,
    'show_in_nav_menus'=>true,
    'query_var'=>'dish',
    'rewrite'=>true,
    'capability_type'=>'post',
    'hierarchicial'=>false,
    'menu_position'=>5,
    'supports'=>array(
        'title',
        'editor',
        'thumbnail',
        'excerpt',
        'custom-fields',
        'revisions'
    )   
);  

register_post_type('dish', $args);

An example of one of the custom taxonomies I want to use is this:

register_taxonomy('Main Ingredient', array('dish'), array(
    'hierarchical' => true,
    'label' => 'Main Ingredient',
    'singular_label' => 'Main Ingredient',
    'query_var'=>true,
    'rewrite' => true)
);

The custom taxonomies are working fine in the admin, and I can go to myurl.com/main-ingredient/pork and see a list of all dishes with pork in them.

What I'm wanting to do is be able to hit myurl.com/main-ingredient and get a list of all the various main-ingredient values.

I found this reference, which is exactly what I'm trying to do: Custom taxonomy listing page when no term set (all terms)

But the solution is not working for me - I'm still getting a 404 when going to myurl.com/main-ingredient

Any suggestions on how best to do this?

share|improve this question
add comment

1 Answer

Hi @Josh:

There is nothing built-in to WordPress to provide an "index" page for your taxonomy as your question implies there should be (and I agree, there should be! But there isn't.)

Instead you have to hack it and one way to do that is to create a page called "Main Ingredient" with a main-ingredient URL slug and assign it a page template for your theme that you will create (maybe) called "Main Ingredient List":

Screenshot showing where to set Page Template in WordPress

Here's a starting point; maybe use the file name page-main-ingredient-list.php for your page template:

<?php
/*
Template Name: Main Ingredient List
*/
get_header();
$main_ingredients = get_terms('main-ingredient');
foreach($main_ingredients as $main_ingredient) {
  $dishes = new WP_Query(array(
    'post_type' => 'dish',
    'post_per_page'=>-1,
    'taxonomy'=>'main-ingredient',
    'term' => $main_ingredient->slug,
  ));
  $link = get_term_link(intval($main_ingredient->term_id),'main-ingredient');
  echo "<h2><a href=\"{$link}\">{$main_ingredient->name}</a></h2>";
  echo '<ul>';
  while ( $dishes->have_posts() ) {
    $dishes->the_post();
    $link = get_permalink($post->ID);
    $title = get_the_title();
    echo "<li><a href=\"{$link}\">{$title}</a></li>";
  }
  echo '</ul>';
}
get_footer();

And then here's what the page looks like with some dummy data on my test site:

Screenshot of a Taxonomy Index page for a WordPress Site

-Mike

share|improve this answer
    
Hi Mike, yes I did do that. It doesn't help. –  user2814 Jan 30 '11 at 5:25
    
@Josh - I added the code you find here to my theme's functions.php file and then went to http://wp30.dev/main-ingredient/chicken/ (wp30.dev is my local test site) and got this screenshot. So if it's not working you have some plugin or some other code that is causing it not to work. –  MikeSchinkel Jan 30 '11 at 5:34
    
Thanks for the reply Mike. That does work for me, but is not the problem I'm trying to solve. I'm trying to get a listing of the main ingredients by going to the top level of the taxonomy. In other words, if you eliminate "chicken" from your URL, you should get a 404 page. That's the problem I'm running into. –  user2814 Jan 30 '11 at 5:48
    
@Josh - Okay, I see that now. Sorry, I've been answering too many questions today. FYI, there is nothing "built-in" way in WordPress to handle what you are asking for. It would make lots of sense for it to be there automatically, but currently it is not. I'll write an update which is a workaround. –  MikeSchinkel Jan 30 '11 at 5:53
3  
@Josh - "but I hate losing the great built-in functionality of the post viewing and navigation via taxonomy term": I must be missing something because I don't see how you loose anything with this? –  MikeSchinkel Jan 30 '11 at 7:04
show 7 more comments

Your Answer

 
discard

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