You’re describing the default behaviour. Based on your code, you will have the following URLs:
- https://www.url.com/blog/addresses/ The archive of all Articles.
- https://www.url.com/blog/city/tunis/ The archive of all Articles assigned to the Tunis City.
- https://www.url.com/blog/addresses/article-title/ The single Article.
If any of these URLs are returning a 404, you likely just need to flush your permalinks. This needs to be done once after registering a post type for the first time. Visiting the Settings > Permalinks page is sufficient to do this (you don’t even need to press save, unless you make a change on the page).
As for the templates to use:
- https://www.url.com/blog/addresses/ Will use
archive-article.php
, if it exists,archive.php
if it doesn’t, andindex.php
if that doesn’t exist either. - https://www.url.com/blog/city/tunis/ Will use
taxonomy-city.php
, if it exists,taxonomy.php
if it doesn’t,archive.php
if that doesn’t exist, andindex.php
if none of those exist.
You can see an explanation and visualisation of the template hierarchy here.
An important thing to remember is that the templates for these all need to use the same main loop as all other templates:
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display post content
endwhile;
endif;
?>
WordPress will automatically query the correct posts to be displayed by this loop, so you should not be using a custom WP_Query
to query posts in these templates.