0

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?

  • 1
    Have you gone to Settings > Permalinks and clicked Save Changes? You must do that once after defining a new tax or CPT. – Peter HvD Aug 8 at 11:15
  • what @Peter says AND you have to register your taxonomy BEFORE the post type, otherwise you can't make them work together in an URL – honk31 Aug 8 at 14:42
  • and in your register_post_type you define 'taxonomies' => array('category'), but where is your taxonomy city..? shouldn't it say 'taxonomies' => array('category', 'city') or 'taxonomies' => array('city')? – honk31 Aug 8 at 14:45
0

You're describing the default behaviour. Based on your code, you will have the following URLs:

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:

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.