1
votes

I have a custom post type named cities. For this custom post type I will have N cities related.

I'm trying to create the files for theses pages and I thought this would work:

single.php - the generic one

single-cities.php - To list the cities

single-cities-{city-slug}.php - To bring the information for THE city


When I try:

/cities - It's showing the single.php content, not the single-cities content with the list.

/cities/sao-paulo - It's working. It shows the information about Sao Paulo.


Test:

If I delete the single-cities-{city-slug}.php, and try to access /cities/sao-paulo, it will bring the single-cities.php


What am I doing wrong in this hierarchy?

My code to register the custom post type:

$city_args = array(
  'labels'              => $labels,
  'public'              => true,
  'has_archive'         => true,
  'publicly_queryable'  => true,
  'query_var'           => true,
  'capability_type'     => 'post',
  'hierarchical'        => false,
  'supports'             => array(
    'title'
  ),
  'exclude_from_search' => false,
  'menu_icon'           => 'dashicons-building'
);

register_post_type('cities', $city_args);

Thanks!


UPDATE

I think I misunderstood the concept of a single page. The single.php is just to render a single post, not to list the posts for a custom post type.

Reading more about this hierarchy, I'm trying to understand how my structure of listing cities and then bring the city information would fit.

Maybe I should create a page for cities, and then inside this page (page-cities.php), create the code to bring all the cities.

Clicking on each city, it should go to the single.php, is that it??

2
add this code to other theme and try.Dev Danidhariya

2 Answers

2
votes

You'll want archive-cities.php to list all cities, single-cities for one post. Use this as a reference: https://codex.wordpress.org/Post_Type_Templates

1
votes

The page listing all your posts is called an Archive, which lives at the slug declared in the register_post_type arguments.

/cities - This is your archive page, so it will call archive-cities.php

/cities/sao-paulo - This is a single post. It will first look for single-cities-{city-slug}.php, then for single-cities.php.

In most cases, you would only use single-cities-{city-slug}.php to make a unique template file for a specific city. Assuming every city post uses an identical layout, you only need single-cities.php.