Display CPT taxonomies as an archive page

by Amor Armand   Last Updated August 08, 2019 10:08 AM

Hello Stack Exchange people,

I created a custom post type called "articles". I also created a taxonomy "city" for that specific post type.

I can create a custom post and set a city to it and it gives me the url I'm wishing to read this page.

https://www.url.com/blog/city/title_of_the_article/

The thing I wish to do is to create a city page that will act like the regular archive.php but just for the city of the custom post types.

For example, I created a CPT for articles with a city set to "Tunis" so my url is:

https://www.url.com/blog/tunis/my_article/

I would like this url:

https://www.url.com/blog/tunis/

to be the archive page for the article with the city set to Tunis.

I ran multiple tests but it didn't work (404 error).

For info, I don't have a archive-city.php page created.

Here is my code for CPT and the taxonomy. (For now and considering the code I'm showing, the archive page is available at: https://www.url.com/blog/articles/)

register_post_type( 'article', 
array( 'labels' => array(
    'name' => 'Articles', 
    'singular_name' => 'Article', 
    'all_items' => 'All article', 
    'add_new' => 'Add article', 
    'add_new_item' => 'Add article',
    'edit' => 'Edit article', 
    'edit_item' =>  'Edit article', 
    'new_item' => 'New article', 
    'view_item' => 'View article', 
    'search_items' => 'Search article', 
    'not_found' =>  'Nothing found in the Database.', 
    'not_found_in_trash' => 'Nothing found in Trash',
    'parent_item_colon' => ''
), 

'description' => 'This is the example custom post type', 
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'query_var' => true,
'menu_position' => 10, 
'menu_icon' => 'dashicons-welcome-write-blog', 
'rewrite'   => array( 'slug' => 'adresses', 'with_front' => true ), 
'has_archive' => 'articles', 
'capability_type' => 'post',
'hierarchical' => false,
'taxonomies' => array( 'category' ),


'supports' => array( 'title', 'editor', 'author', 'thumbnail' ))); 

and

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

To get https://www.url.com/blog/city/ as the archive page, what am I missing?

Thanks



Related Questions





Custom Taxonomy - Using URL Parameters to Filter Posts

Updated October 14, 2018 04:08 AM