Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Create directories wordpress

Hi there, From a week I moved from Joomla to Wordpress and I'm happily following the threehouse wordpress development course but I'm facing a big problem and I can't figure out how to solve it. I'm working on a site that needs directories like: Geographical Regions (Asia, Europe, America etc..) click on Europe and see a list of countries (Germany, France, Spain etc..) click on a country and see a list of cities and any city has it's own page. I'm actually using Custom Post types plugin to create Cities and Advanced Custom Fields to customize the single city page. Assigning the new template to a page I can create the path Country -> City. How can I create the rest of the directory? I'm not familiar with categories and tags in Wordpress. I see that there are plugins that could help to achieve this but would make not possible to use custom post types and custom fields. Is there a way to do this directory structure using only categories, tags and custom post types and custom fields?

Thank you so much for your time!

3 Answers

Apologies if you've done any of this already, but something like the following will allow you to get the required result:

  1. Create custom taxonomies using the CPT UI plugin, and assign them to your custom post types. A taxonomy is just a set of terms, and WP allows you to group them together (e.g. the "Cities" taxonomy could have taxonomy terms of London, Berlin, Paris. A seperate "Countries" taxonomy would be seperate, and have taxonomy terms of England, Germany and France).

  2. Create single-city.php and single-country.php files to display the content for each page as required. This structure is standard WP for custom post types. Just as single.php picks up regular posts, single-city.php would pick up posts of the type "city".

  3. Create at least one page template to display all content, and a customisable search option using WP_Query and PHP variables, assigned to match ACF values. For example, you could create a page template that picks up ACF values for city, country and region. These would be set as variables on your PHP files (something like $city = get_field('city');), and then queried in your WP_Query using something like:

$args = array( 'post_type' => 'georgraphy'; 'tax_query' => $city; )

$query = new WP_Query($args);

(If you don't know how to do this, Zac Gordon has an excellent 30ish minute workshop video).

That should do it! I would run through the Treehouse courses some more, specifically focussing on writing page templates, creating custom post types and learning WP_query inside out.

Mike

Sue Dough
Sue Dough
35,800 Points

Hi Andrea,

You could use categories with a custom post type. Then use child categories to create the hiarchy. I have read before that having a few thousand categories on WordPress can cause some slowness. It may be over kill too.

It may be simpler to do 1 template and no categories. It could use get paramaters to find out what the user is looking for.

domain.com/?geo_location=africa
if ( isset( $_GET['geo-region'] ) {

  // sanitize and validate

  // call function to get countries for region

}

I can't write all the code for you however hopefully that should give you an idea.

Sorry for the late answer! Thanks for the precious advices, I finally got the result!

Have a great day!