最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom taxonomy list page?

programmeradmin0浏览0评论

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/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/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.

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

Any suggestions on how best to do this?

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/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/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.

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

Any suggestions on how best to do this?

Share Improve this question edited Jun 10, 2017 at 9:52 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Jan 30, 2011 at 4:09 user2814user2814
Add a comment  | 

1 Answer 1

Reset to default 20

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":


(source: mikeschinkel)

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:

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论