Search code examples
phpwordpresstemplatestaxonomy

Template for Custom Taxonomy


I spent my much time to find the solution but no luck .

i a have custom Post Type called recipe which have recipe types (Taxonomies) eg A, B, C

now i want to list down all the recipes from type A on www.mydomain.com/recipes/A

my custom post type is : recipe_cpt and Taxonomy is recipe_tx

i have tried taxonomy.php, taxonomy-recipe_tx.php but i am getting 404.

can some one help me how to create a template for custom taxonomy .

here is my custom taxonomy

function reg_recipe_taxtaxonomy() {

$labels = array(
    'name'                       => _x( 'Recipe Categories', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Recipe Category', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Recipe Types', 'text_domain' ),
    'all_items'                  => __( 'All Types', 'text_domain' ),
    'parent_item'                => __( 'Parent Type', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Type:', 'text_domain' ),
    'new_item_name'              => __( 'New Type Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Type', 'text_domain' ),
    'edit_item'                  => __( 'Edit Type', 'text_domain' ),
    'update_item'                => __( 'Update Type', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate Types with commas', 'text_domain' ),
    'search_items'               => __( 'Search Types', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove Types', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used Types', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
);
$rewrite = array(
        'slug'                       => 'recipes',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'rewrite'                    => $rewrite,
);
register_taxonomy( 'recipe_tx', array( 'recipe_cpt' ), $args );

   }

      // Hook into the 'init' action
     add_action( 'init', 'reg_recipe_taxtaxonomy', 0 );

Solution

  • i found the solution . creating template for custom taxonomy .

    if you are rewriting the url of custom taxonomy

    $rewrite = array(
            'slug'                       => 'recipes',
            'with_front'                 => true,
            'hierarchical'               => true,
        );
    

    then you must Remove and recreate rewrite rules. for this i found a function on Codex

    just call flush_rewrite_rules() after register_taxonomy()

    the final code will be

    function reg_recipe_taxtaxonomy() {
    
    $labels = array(
        'name'                       => _x( 'Recipe Categories', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Recipe Category', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Recipe Types', 'text_domain' ),
        'all_items'                  => __( 'All Types', 'text_domain' ),
        'parent_item'                => __( 'Parent Type', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent Type:', 'text_domain' ),
        'new_item_name'              => __( 'New Type Name', 'text_domain' ),
        'add_new_item'               => __( 'Add New Type', 'text_domain' ),
        'edit_item'                  => __( 'Edit Type', 'text_domain' ),
        'update_item'                => __( 'Update Type', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate Types with commas', 'text_domain' ),
        'search_items'               => __( 'Search Types', 'text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove Types', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used Types', 'text_domain' ),
        'not_found'                  => __( 'Not Found', 'text_domain' ),
    );
    $rewrite = array(
            'slug'                       => 'recipes',
            'with_front'                 => true,
            'hierarchical'               => true,
        );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'rewrite'                    => $rewrite,
    );
    register_taxonomy( 'recipe_tx', array( 'recipe_cpt' ), $args );
    flush_rewrite_rules();
    }
    
          // Hook into the 'init' action
         add_action( 'init', 'reg_recipe_taxtaxonomy', 0 );
    

    hope it will help someone !