0

I'm trying to set up my custom template for a custom taxonomy. When I click on a category link to display the custom posts in that specific category, I get the 404 page. My custom post type file name is template-features.php and my taxonomy archive file is archive-features.php. Here is my code to register the taxonomy and post type:

Custom post type and taxonomy:

add_action( 'init', 'post_type_features', 0 );

function post_type_features() {

    $labels = array(
        'name'                => _x( 'Features', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Feature', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Features', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Feature:', 'text_domain' ),
        'all_items'           => __( 'All Features', 'text_domain' ),
        'view_item'           => __( 'View Feature', 'text_domain' ),
        'add_new_item'        => __( 'Add New Feature', 'text_domain' ),
        'add_new'             => __( 'Add New', 'text_domain' ),
        'edit_item'           => __( 'Edit Feature', 'text_domain' ),
        'update_item'         => __( 'Update Feature', 'text_domain' ),
        'search_items'        => __( 'Search Features', 'text_domain' ),
        'not_found'           => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
    );
    $args = array(
        'label'               => __( 'post_features', 'text_domain' ),
        'description'         => __( 'White Glove Labs Features', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'thumbnail', 'custom-fields', ),
        'taxonomies'          => array( 'Features' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_icon'           => 'dashicons-star-empty',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );
    register_post_type( 'post_features', $args );

}

add_action( 'init', 'lv_features_taxonomy', 0 );

function lv_features_taxonomy() {

    $labels = array(
    'name' => _x( 'Features', 'Features Taxonomy' ),
    'singular_name' => _x( 'Feature', 'Feature singular name' ),
    'search_items' =>  __( 'Search Features' ),
    'all_items' => __( 'All Features' ),
    'parent_item' => __( 'Parent Feature' ),
    'parent_item_colon' => __( 'Parent Feature:' ),
    'edit_item' => __( 'Edit Feature' ), 
    'update_item' => __( 'Update Feature' ),
    'add_new_item' => __( 'Add New Feature' ),
    'new_item_name' => __( 'New Feature Name' ),
    'menu_name' => __( 'Features Categories' ),
    );  

    register_taxonomy('features',array('post_features'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'features' ),
    ));

}

Just for testing, I have a very basic custom archive file. Here is the code:

    <?php get_header(); ?>
<div id="main-content" class="main-content"> 
    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <header class="archive-header">
                <h1 class="archive-title">
                    <?php post_type_archive_title(); ?>
                </h1>
            </header>           
        </div>
    </div>
</div>
<?php
get_sidebar();
get_footer();

2 Answers 2

1

From code for post type and custom taxonomy, your CPT is post_features and taxonomy is features.

For single post_features, you need to have file named single-post_features.php. For archive, create file taxonomy-features.php.

See documentation for detail. https://codex.wordpress.org/Post_Type_Templates

Example for single(single-post_features.php):

<?php get_header(); ?>
<?php
if ( have_posts() ) {
  while ( have_posts() ) {
    the_post();

    the_title();
    the_content();
  } // end while
} // end if
?>
<?php get_footer(); ?>

Example for Archive(taxonomy-features.php):

<?php get_header(); ?>
<?php
if ( have_posts() ) {
  the_archive_title( '<h1>', '</h1>' );
  while ( have_posts() ) {
    the_post();
    the_title();
    the_content();
  } // end while
} // end if
?>
<?php get_footer(); ?>

Note: Please flush your permalink if you are getting 404 error.

0
0

Did you remember to flush the rewrite rules after creating your custom post type? Visit Settings > Permalinks and hit save. ( No need to change anything)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.