0

I have registered a custom post type city with 2 taxonomies city_categories and cities. But wordpress is taking archive.php instead of archive-city.php, don't understand where am I mistaking. It works only if I create a taxonomy-cities.php and then is taking taxonomies-cities.php. How can I make it take directly archive-city.php

register_post_type( 'city',
    array(
        'labels' => 
            array(
            'name' => 'City Guides',
            'singular_name' => 'City Guides',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New retailer',
            'edit' => 'Edit',
            'edit_item' => 'Edit retailer',
            'new_item' => 'New retailer',
            'view' => 'View',
            'view_item' => 'View retailer',
            'search_items' => 'Search retailers',
            'not_found' => 'No retailer found',
            'not_found_in_trash' => 'No Shop retailer in Trash',
            'parent' => 'Parent retailer'
        ),
        'public' => true,
        'menu_position' => 6,
        'supports' => array( 'title', 'editor', 'thumbnail' ),
        'has_archive' => true,
        'taxonomies' => array('city_categories'),
        'can_export' => true,
        'rewrite' => array('slug'=>'travel')
    )
);  

$args = array( 
    'labels' => $labels, // defined correctly, shortened the code
    'public' => true,
    'show_in_nav_menus' => true,
    'show_ui' => true,
    'show_tagcloud' => true,
    'show_admin_column' => true,
    'hierarchical' => true,
    'rewrite' => true,
    'query_var' => true
);

register_taxonomy( 'city_categories', array('city'), $args );

$args = array(
    'labels' => $labels, // defined correctly, shortened the code
    'public' => true,
    'show_in_nav_menus' => true,
    'show_ui' => true,
    'show_tagcloud' => true,
    'show_admin_column' => true,
    'hierarchical' => true,
    'rewrite' => true,
    'query_var' => true,
    'sort' => true
);

register_taxonomy( 'cities', array('city'), $args );
9
  • Why have you named the taxonomy exactly like the post type?
    – kaiser
    Commented Aug 31, 2014 at 13:07
  • @kaiser Where is that, they are different. Taxonomies are city_categories and cities, and post type is city.
    – Adrian
    Commented Aug 31, 2014 at 13:11
  • Verify that template is named precisely what you think it's named and resides in directory you think it's residing. :) Any other code (plugins/etc) running that might be interfering?
    – Rarst
    Commented Aug 31, 2014 at 13:54
  • Have you tried refreshing the permalinks?
    – Tomás Cot
    Commented Aug 31, 2014 at 14:51
  • @TomásCot How to do that?
    – Adrian
    Commented Aug 31, 2014 at 15:43

2 Answers 2

0

I've tested your code and it works perfectly if you register the post type in the init action hook. In your code I don't see the action hook where you register the post type. Cna you try this? (remember to flush rewirte rules after update the code. Note that I remove the undefined variable $labels in the $args array for taxonomies):

add_action('init', 'cyb_register_post_type_and_taxonomies');
function cyb_register_post_type_and_taxonomies() {
    register_post_type( 'city',
                        array(
                            'labels' => 
                                array(
                                    'name' => 'City Guides',
                                    'singular_name' => 'City Guides',
                                    'add_new' => 'Add New',
                                    'add_new_item' => 'Add New retailer',
                                    'edit' => 'Edit',
                                    'edit_item' => 'Edit retailer',
                                    'new_item' => 'New retailer',
                                    'view' => 'View',
                                    'view_item' => 'View retailer',
                                    'search_items' => 'Search retailers',
                                    'not_found' => 'No retailer found',
                                    'not_found_in_trash' => 'No Shop retailer in Trash',
                                    'parent' => 'Parent retailer'
                                ),
                            'public' => true,
                            'menu_position' => 6,
                            'supports' => array( 'title', 'editor', 'thumbnail' ),
                            'has_archive' => true,
                            'taxonomies' => array('city_categories'),
                            'can_export' => true,
                            'rewrite' => array('slug'=>'travel')
                         )
    );
    $args = array( 
            'labels' => array(),
            'public' => true,
            'show_in_nav_menus' => true,
            'show_ui' => true,
            'show_tagcloud' => true,
            'show_admin_column' => true,
            'hierarchical' => true,
            'rewrite' => true,
            'query_var' => true
    );

    register_taxonomy( 'city_categories', array('city'), $args );

    $args = array(
            'labels' => array(), // defined correctly, shortened the code
            'public' => true,
            'show_in_nav_menus' => true,
            'show_ui' => true,
            'show_tagcloud' => true,
            'show_admin_column' => true,
            'hierarchical' => true,
            'rewrite' => true,
            'query_var' => true,
            'sort' => true
    );

    register_taxonomy( 'cities', array('city'), $args );
}
0

Still sounds something went wrong with your rewrite rules, try adding flush_rewrite_rules() at the end of your function that contains the registrations calls you posted in your questions. Like this:

flush_rewrite_rules( false );

Setting the parameter $hard to false means you won't update the .htaccess and just do a update of the rewrite_rules transient. Should be enough though.

But do not forget to remove this line from your code after testing, because otherwise it will be executed every time your site is called, which is bad and you certainly don't want that.

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.