0

I have a custom post type called "News" which looks like this:

/**
 * Register "News" custom post type
 */
function news_custom_post_type() {

    $labels = array(
        'name'                  => _x( 'News', 'Post Type General Name', 'school' ),
        'singular_name'         => _x( 'News Item', 'Post Type Singular Name', 'school' ),
        'menu_name'             => __( 'News', 'school' ),
        'name_admin_bar'        => __( 'News', 'school' ),
        'archives'              => __( 'News Archives', 'school' ),
        'attributes'            => __( 'News Attributes', 'school' ),
        'parent_item_colon'     => __( 'Parent Post:', 'school' ),
        'all_items'             => __( 'All Posts', 'school' ),
        'add_new_item'          => __( 'Add a New Post', 'school' ),
        'add_new'               => __( 'Add New', 'school' ),
        'new_item'              => __( 'New Post', 'school' ),
        'edit_item'             => __( 'Edit Post', 'school' ),
        'update_item'           => __( 'Update Post', 'school' ),
        'view_item'             => __( 'View Post', 'school' ),
        'view_items'            => __( 'View Posts', 'school' ),
        'search_items'          => __( 'Search Posts', 'school' ),
        'not_found'             => __( 'Not found', 'school' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'school' ),
        'featured_image'        => __( 'Featured Image', 'school' ),
        'set_featured_image'    => __( 'Set featured image', 'school' ),
        'remove_featured_image' => __( 'Remove featured image', 'school' ),
        'use_featured_image'    => __( 'Use as featured image', 'school' ),
        'insert_into_item'      => __( 'Insert into post', 'school' ),
        'uploaded_to_this_item' => __( 'Uploaded to this post', 'school' ),
        'items_list'            => __( 'Posts list', 'school' ),
        'items_list_navigation' => __( 'Posts list navigation', 'school' ),
        'filter_items_list'     => __( 'Filter posts list', 'school' ),
    );
    $args = array(
        'label'                 => __( 'News Post', 'school' ),
        'description'           => __( 'A custom post type for news posts.', 'school' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt' ),
        'taxonomies'            => array( 'news' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'news', $args );

}
add_action( 'init', 'news_custom_post_type', 0 );

I've also set up taxonomies for this post type:

/**
 * Register "News" Categories
 */
function news_categories() {

    $labels = array(
        'name'                       => _x( 'Categories', 'Taxonomy General Name', 'school' ),
        'singular_name'              => _x( 'Category', 'Taxonomy Singular Name', 'school' ),
        'menu_name'                  => __( 'Categories', 'school' ),
        'all_items'                  => __( 'All Items', 'school' ),
        'parent_item'                => __( 'Parent Category', 'school' ),
        'parent_item_colon'          => __( 'Parent Category:', 'school' ),
        'new_item_name'              => __( 'New Category Name', 'school' ),
        'add_new_item'               => __( 'Add New Category', 'school' ),
        'edit_item'                  => __( 'Edit Category', 'school' ),
        'update_item'                => __( 'Update Category', 'school' ),
        'view_item'                  => __( 'View Category', 'school' ),
        'separate_items_with_commas' => __( 'Separate categories with commas', 'school' ),
        'add_or_remove_items'        => __( 'Add or remove categories', 'school' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'school' ),
        'popular_items'              => __( 'Popular Categories', 'school' ),
        'search_items'               => __( 'Search Categories', 'school' ),
        'not_found'                  => __( 'Not Found', 'school' ),
        'no_terms'                   => __( 'No items', 'school' ),
        'items_list'                 => __( 'Categories list', 'school' ),
        'items_list_navigation'      => __( 'Categories list navigation', 'school' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'news', array( 'news' ), $args );

}
add_action( 'init', 'news_categories', 0 );

When I visit one of the categories, I get a 404. E.g. news/category. Do I need a special function within archive.php or a separate taxonomy template?

Here's a simplified version of archive.php that I'm running:

<?php

  // Begin main loop
  if ( have_posts() ):

  // Begin main loop while
  while ( have_posts() ): the_post();

    // Posts go here

  // End main loop while
  endwhile;

  // End main loop
  endif;

?>

I've tried flushing my permalinks, but this doesn't do the trick. Category base settings are also empty:

Category base

  • Have you flushed your permalinks? Just visit Settings > Permalinks and try again. – Jacob Peattie Oct 3 '18 at 15:05
  • I have :) Updated question. – Sam Oct 3 '18 at 15:05
  • 2
    you can't have both a post type and taxonomy with the same slug, you've named them both news. – Milo Oct 3 '18 at 15:14
  • 2
    I just noticed, you've used that same name for the post type and the taxonomy. They need to be different. Otherwise WordPress can't tell if news/category is a news taxonomy term named category or a news post named category. – Jacob Peattie Oct 3 '18 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.