Display CPT taxonomies as an archive page

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.example.com/blog/city_name/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 an article with a city set to “Tunis” so my URL is:

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

I would like this url: https://www.example.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.example.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.example.com/blog/city/ as the archive page, what am I missing?

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Display CPT taxonomies as an archive page

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.