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

Noah Laurent
Noah Laurent
3,080 Points

How to plan a wordpress category based site without conflicting categories pages and tags? Is multisite the way to go?

Hello,

Im planning a local tourist guide for few cities in my country but I can’t figure out the website planning for this project. Here it goes:

My ideal Url strategy is: tour.pt/city/category/sub-category

If /city/ is itself category I have a problem, because will be a category conflict, for example: tour.pt/city-1/what-to-do/sub-category tour.pt/city-2/what-to-do/sub-category

I cant have /what-to-do/ category to both city-1 and city-2 category hierarchy..

Should the /city/ be a individual page with a custom menu for each? Even this way I would have a category conflict and I would have to change the category name for each place..

Should I go multisite subdomain style: city-1.tour.pt/category? That doesnt make much sense since it’s a small project with little content per city/local, but it would work.

This is another pratical example tour.pt/algarve/where-to-stay/hotels/best-hotels-in-algarve being: home.pt/category-1/category-1.1/category-1-1-1/post Should this be: home.pt/page-1/category-1/category-1.1/post

This way I will get a messy content conflict, because “hotels” is a shared category, that can have diferent parents, because It suits all cities.

I really don’t know out to proceed beacause I only worked in single topic/product/company projects before, and since now I can have many topics (cities) I dont know what strategy is best.

Check these ones out for better picture: http://imgur.com/Xiz62Vr http://imgur.com/NVL0URP

Thanks in advance, Laurent

1 Answer

Gabriel Tartaglia
Gabriel Tartaglia
41,581 Points

Hi Laurent,

I guess the best option is register a new taxonomy for cities.

Here is one way that you can do it:

  1. Download, install and activate the Custom Post Type UI plugin
  2. Register a new taxonomy at CPT UI > Add/Edit Taxonomies. Fill the fields with city, City, Cities (or whatever you like) and check Posts.
  3. Create some cities at Posts > Cities
  4. Set your permalink to /%city%/%category%/%postname%. Replace %city% with the slug of your custom taxonomy.
  5. It is not working yet. You need to add a filter hook in your code to tell WordPress what exactly it needs to do with this %city% thing. I prepared the code for you, just put it in the functions.php of your theme or plugin.
function gotreehouse_custom_permalink($permalink, $post_id, $leavename) {
    $default_slug = 'anywhere';  // In case the post do not have a city
    $the_slug       = 'city';            // The slug you choose for your taxonomy

    if ( false === strpos( $permalink, "%$the_slug%" ) ) return $permalink;

    $post = get_post( $post_id );
    if ( ! $post ) return $permalink;

    $terms = wp_get_object_terms( $post->ID, $the_slug );
    if ( ! is_wp_error( $terms ) && ! empty( $terms ) && is_object( $terms[0] ) )
        $taxonomy_slug = $terms[0]->slug;
    else
        $taxonomy_slug = $default_slug;

    return str_replace( "%$the_slug%", $taxonomy_slug, $permalink );
}
add_filter( 'post_link', 'gotreehouse_custom_permalink', 10, 3 );

Take a special look at the first and second line of the function. The default_slug is used in case your post does not have a city checked, will be like yousite.com/default-slug/hotels/the-post and the_slug is simply the slug you choose at the time you create your taxonomy.

After that, you should have the expected behaviour.

I hope this helps with your problem.