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

custom post types - CPT Taxonomy As Archive Page – Error 404

programmeradmin0浏览0评论

I have a CPT called Clubs. To 'categorise' posts depending on if clubs are in the same league or cup competition (or neither), I also have a custom taxonomy called Opposition.

To show which clubs are in either the same competition, I am trying to create a page that functions like an Archive page where these specific Clubs posts are displayed: /clubs/opposition/

To do this, I've created a taxonomy-opposition.php template file and edited the functions.php file with the following:

// Clubs Post Type
function clubs_init() {
    $labels = array(
        'name' => 'Clubs',
        'singular_name' => 'Club',
        'add_new' => 'Add New Club',
        'add_new_item' => 'Add New Club',
        'edit_item' => 'Edit Club',
        'new_item' => 'New Club',
        'all_items' => 'All Clubs',
        'view_item' => 'View Club',
        'search_items' => 'Search Clubs',
        'not_found' =>  'No Clubs Found',
        'not_found_in_trash' => 'No Clubs found in Trash', 
        'parent_item_colon' => '',
        'menu_name' => 'Clubs',
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => false,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'taxonomies'  => array( 'clubs', 'opposition' ),
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'clubs',
            'with_front' => false
        ),
        'menu_icon' => 'dashicons-shield',
        'supports' => array(
        'title',
        'editor',
        'excerpt',
        'trackbacks',
        'custom-fields',
        'comments',
        'revisions',                
        'thumbnail',
        'author',
        'page-attributes'
        )
    );
    register_post_type( 'clubs', $args );
}
add_action( 'init', 'clubs_init' );

// Opposition Taxonomy
function opposition_taxonomy() {
    $labels = array(
        'search_items'      => 'Search Opposition',
        'menu_name'         => 'Opposition'
    );
    register_taxonomy( 'opposition', 'clubs', 
    array(
        'hierarchical' => true,
        'labels' => $labels,
        'query_var' => true,
        'show_admin_column' => true,
        'has_archive' => true,
        'rewrite' => array(
        'slug' => 'clubs',
        'with_front' => false
    ),  
) );
}
add_action( 'init', 'opposition_taxonomy' );

The result when I try to access /clubs/opposition, though, is an Error 404. The thing baffling me is that I've done something similar for my Players CPT, creating a custom Squad taxonomy page that does display. And I've followed exactly the same steps in trying to create an Opposition page.

(I have flushed the permalinks)

I've referred to existing questions, but so far haven't found the solution:

  • Custom taxonomy archive page not working
  • Custom taxonomy template not working
  • custom taxonomy - Template not working
  • Taxonomy page returns 404 page not found

I have a CPT called Clubs. To 'categorise' posts depending on if clubs are in the same league or cup competition (or neither), I also have a custom taxonomy called Opposition.

To show which clubs are in either the same competition, I am trying to create a page that functions like an Archive page where these specific Clubs posts are displayed: /clubs/opposition/

To do this, I've created a taxonomy-opposition.php template file and edited the functions.php file with the following:

// Clubs Post Type
function clubs_init() {
    $labels = array(
        'name' => 'Clubs',
        'singular_name' => 'Club',
        'add_new' => 'Add New Club',
        'add_new_item' => 'Add New Club',
        'edit_item' => 'Edit Club',
        'new_item' => 'New Club',
        'all_items' => 'All Clubs',
        'view_item' => 'View Club',
        'search_items' => 'Search Clubs',
        'not_found' =>  'No Clubs Found',
        'not_found_in_trash' => 'No Clubs found in Trash', 
        'parent_item_colon' => '',
        'menu_name' => 'Clubs',
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => false,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'taxonomies'  => array( 'clubs', 'opposition' ),
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'clubs',
            'with_front' => false
        ),
        'menu_icon' => 'dashicons-shield',
        'supports' => array(
        'title',
        'editor',
        'excerpt',
        'trackbacks',
        'custom-fields',
        'comments',
        'revisions',                
        'thumbnail',
        'author',
        'page-attributes'
        )
    );
    register_post_type( 'clubs', $args );
}
add_action( 'init', 'clubs_init' );

// Opposition Taxonomy
function opposition_taxonomy() {
    $labels = array(
        'search_items'      => 'Search Opposition',
        'menu_name'         => 'Opposition'
    );
    register_taxonomy( 'opposition', 'clubs', 
    array(
        'hierarchical' => true,
        'labels' => $labels,
        'query_var' => true,
        'show_admin_column' => true,
        'has_archive' => true,
        'rewrite' => array(
        'slug' => 'clubs',
        'with_front' => false
    ),  
) );
}
add_action( 'init', 'opposition_taxonomy' );

The result when I try to access /clubs/opposition, though, is an Error 404. The thing baffling me is that I've done something similar for my Players CPT, creating a custom Squad taxonomy page that does display. And I've followed exactly the same steps in trying to create an Opposition page.

(I have flushed the permalinks)

I've referred to existing questions, but so far haven't found the solution:

  • Custom taxonomy archive page not working
  • Custom taxonomy template not working
  • custom taxonomy - Template not working
  • Taxonomy page returns 404 page not found
Share Improve this question asked Jul 16, 2019 at 5:46 Pete HaymanPete Hayman 719 bronze badges 1
  • 1 There's nothing for WordPress to display on /clubs/opposition. WordPress only lists posts on archives, and /clubs/opposition isn't specific enough to list any posts. You need to create Opposition terms, and then you can list posts with those terms at /clubs/opposition/term-name. – Jacob Peattie Commented Jul 16, 2019 at 6:15
Add a comment  | 

1 Answer 1

Reset to default 1

You use the same slug clubs to register your custom post type and in rewrite when you register opposition taxonomy.

Change rewrite parameter in register_taxonomy()

'rewrite' => array(
    'slug' => 'clubs/opposition',
    'with_front' => false
),

The recognizable address will be clubs/opposition/{some-term-name} and will display posts from custom category.

However, the clubs/opposition address will still not work without additional code.

发布评论

评论列表(0)

  1. 暂无评论