admin管理员组

文章数量:1410717

I have created a custom post type 'item':

function mywp_add_item_post() {
    $labels = array(
        'name' => 'Items',
        'singular_name' => 'Item',
        'add_new' => 'Add New Item',
        'add_new_item' => 'Add New Item',
        'edit_item' => 'Edit Item',
        'new_item' => 'New Item',
        'all_items' => 'All Items',
        'view_item' => 'View Item',
        'search_items' => 'Search Items',
        'not_found' =>  'No Items Found',
        'not_found_in_trash' => 'No Items found in Trash', 
        'parent_item_colon' => '',
        'menu_name' => 'Items',
        'show_in_nav_menus'=>true
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'item'),
        'query_var' => true,
        'menu_icon' => 'dashicons-randomize',
        'show_in_rest' => true,
        'show_in_nav_menus'=>true,
        'supports' => array('title', 'editor', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'thumbnail', 'author', 'page-attributes'));

    register_post_type( 'item', $args );
  }

add_action( 'init', 'mywp_add_item_post');

A custom taxonomy named 'collection' is added:

function mywp_add_collection_taxonomy() {
      $labels= array('name' => _x( 'Collections', 'taxonomy general name' ),
      'singular_name' => _x( 'Collection', 'taxonomy singular name' ),
      'search_items' =>  __( 'Search Collections' ),
      'all_items' => __( 'All Collection' ),
      'parent_item' => __( 'Parent Collection' ),
      'parent_item_colon' => __( 'Parent Collection:' ),
      'edit_item' => __( 'Edit Collection' ),
      'update_item' => __( 'Update Collection' ),
      'add_new_item' => __( 'Add New Collection' ),
      'new_item_name' => __( 'New Collection Name' ),
      'menu_name' => __( 'Collections' ));

      $rewrite= array('slug' => 'collection');

  $args= array('hierarchical' => true,
      'show_in_nav_menus' => true,
      'has_archive' => true,
      'labels'=>$labels,
      'query_var' => true,
      'show_in_rest' => true,
      'rewrite'=>$rewrite);

   register_taxonomy('collection', 'item', $args);
}

function mywp_tax_filters()
{
    register_taxonomy_for_object_type('collection', 'item');
}

add_action('init', 'mywp_add_collection_taxonomy', 1);
add_action('init', 'mywp_tax_filters');

Object: I need a page to view the list of 'collection' that are not empty (like the default categories page)

  • A taxonomy template file (taxonomy-collection.php) is created within the theme directory.
  • Permalink structure is flushed.
  • I have added some 'collections' assigning 'items' inside them.

Problem: The link '$site_url/collection' shows 404 (not found).

Advance thanks for your thoughts on my problem and your patience to read it all through.

I have created a custom post type 'item':

function mywp_add_item_post() {
    $labels = array(
        'name' => 'Items',
        'singular_name' => 'Item',
        'add_new' => 'Add New Item',
        'add_new_item' => 'Add New Item',
        'edit_item' => 'Edit Item',
        'new_item' => 'New Item',
        'all_items' => 'All Items',
        'view_item' => 'View Item',
        'search_items' => 'Search Items',
        'not_found' =>  'No Items Found',
        'not_found_in_trash' => 'No Items found in Trash', 
        'parent_item_colon' => '',
        'menu_name' => 'Items',
        'show_in_nav_menus'=>true
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'item'),
        'query_var' => true,
        'menu_icon' => 'dashicons-randomize',
        'show_in_rest' => true,
        'show_in_nav_menus'=>true,
        'supports' => array('title', 'editor', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'thumbnail', 'author', 'page-attributes'));

    register_post_type( 'item', $args );
  }

add_action( 'init', 'mywp_add_item_post');

A custom taxonomy named 'collection' is added:

function mywp_add_collection_taxonomy() {
      $labels= array('name' => _x( 'Collections', 'taxonomy general name' ),
      'singular_name' => _x( 'Collection', 'taxonomy singular name' ),
      'search_items' =>  __( 'Search Collections' ),
      'all_items' => __( 'All Collection' ),
      'parent_item' => __( 'Parent Collection' ),
      'parent_item_colon' => __( 'Parent Collection:' ),
      'edit_item' => __( 'Edit Collection' ),
      'update_item' => __( 'Update Collection' ),
      'add_new_item' => __( 'Add New Collection' ),
      'new_item_name' => __( 'New Collection Name' ),
      'menu_name' => __( 'Collections' ));

      $rewrite= array('slug' => 'collection');

  $args= array('hierarchical' => true,
      'show_in_nav_menus' => true,
      'has_archive' => true,
      'labels'=>$labels,
      'query_var' => true,
      'show_in_rest' => true,
      'rewrite'=>$rewrite);

   register_taxonomy('collection', 'item', $args);
}

function mywp_tax_filters()
{
    register_taxonomy_for_object_type('collection', 'item');
}

add_action('init', 'mywp_add_collection_taxonomy', 1);
add_action('init', 'mywp_tax_filters');

Object: I need a page to view the list of 'collection' that are not empty (like the default categories page)

  • A taxonomy template file (taxonomy-collection.php) is created within the theme directory.
  • Permalink structure is flushed.
  • I have added some 'collections' assigning 'items' inside them.

Problem: The link '$site_url/collection' shows 404 (not found).

Advance thanks for your thoughts on my problem and your patience to read it all through.

Share Improve this question asked Nov 11, 2019 at 6:05 sariDonsariDon 2651 gold badge2 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

A missing template will never result in a 404 error. That's not how templates work. The reason you're getting a 404 is that you're attempting to access a URL that does not exist.

{Site URL}/{Taxonomy name}/ doesn't exist. There's nothing for WordPress to display at that URL. WordPress templates only ever display posts, but this URL doesn't specify which posts to display. You need to specify which collection term to display, and then access posts with that term at {Site URL}/{Taxonomy name}/{Term slug}. All posts would be accessible at {Site URL}/{Post type name}/, but {Site URL}/{Taxonomy name}/ will return a 404.

If you want to display a list of terms within a taxonomy, then the only way to do that would be to create a page with a custom template that lists all terms. It's not a native type of template in WordPress.

本文标签: Custom taxonomy template list not working (404)