Home > Software engineering >  WordPress taxanomy.php not Showing
WordPress taxanomy.php not Showing

Time:10-26

I have created Cutom post type and also created custom taxanomy but when i am trying to view any category the page is showing index.php insted of taxanomy.php I also tried taxonomy-portfolio-category.php but its still showing index.php

The portfolio post type was created with the following code:

`

// portfolio Custom Post Type
function portfolio_init()
{
    // set up portfolio labels
    $labels = array(
        'name' => 'Portfolio',
        'singular_name' => 'Portfolio',
        'add_new' => 'Add New Portfolio',
        'add_new_item' => 'Add New Portfolio',
        'edit_item' => 'Edit Portfolio',
        'new_item' => 'New Portfolio',
        'all_items' => 'All Portfolio',
        'view_item' => 'View Portfolio',
        'search_items' => 'Search Portfolio',
        'not_found' =>  'No Portfolio Found',
        'not_found_in_trash' => 'No Portfolio found in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'Portfolio',
    );
    // register post type
    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        "publicly_queryable" => true,
        "hierarchical" => false,
        'rewrite' => array('slug' => 'portfolio', 'with_front' => false),
        'query_var' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'menu_icon' => 'dashicons-layout',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes'
        )
    );
    flush_rewrite_rules();
    register_post_type('portfolio', $args);
    // register taxonomy
    register_taxonomy('portfolio-category', 'portfolio', array(
        '
    hierarchical' => true,
        'label' => 'Portfolio Category',
        'query_var' => true,
        'rewrite' => array('slug' => 'portfolio', 'with_front' => false),
    ));
    //register tags
    register_taxonomy('portfolio_tag', 'portfolio', array(
        'hierarchical' => false,
        'label' => 'Portfolio Tags',
        'show_ui' => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var' => true,
        'rewrite' => array('slug' => 'portfolio', 'with_front' => false),
    ));
    flush_rewrite_rules();
}
add_action('init', 'portfolio_init');

`

I am expecting taxonomy-portfolio-category.php not index.php

CodePudding user response:

You could try to modify the rewrite-rule when you register the taxonomies. At the moment all three; post type and both taxonomies share the same rewrite-rule. They all point to the same rewrite-structure, where the post type wins the race, which is index.php.

New rewrite-rules for the taxonomies:

// register taxonomy
register_taxonomy(
    'portfolio-category',
    'portfolio',
    array(
        'hierarchical' => true,
        'label'        => 'Portfolio Category',
        'query_var'    => true,
        'rewrite'      => array(
            'slug'       => 'portfolio-category',
            'with_front' => false,
        ),
    )
);

//register tags
register_taxonomy(
    'portfolio_tag',
    'portfolio',
    array(
        'hierarchical'          => false,
        'label'                 => 'Portfolio Tags',
        'query_var'             => true,
        'show_ui'               => true,
        'update_count_callback' => '_update_post_term_count',
        'rewrite'               => array(
            'slug'       => 'portfolio-tag',
            'with_front' => false,
        ),
    )
);

If you need to customize the taxonomy-slugs further, ex. portfolio/category/post_name, you'd have to create custom rewrite rules using add_rewrite_rule(), add_rewrite_tag() and some other functions :)

  • Related