<p>Yes, you can totally do this with categories, but then you have to assign a city category to each entry manually. Another idea would be to use a structure section for all of your entries, with top level entries being entries for the cities and child entries for the stories (you can make use of different entry types).</p>

<p>Your cities index page to promt the user for their preferred city would list the categories or the top level entries of your structure. Use a conditional in this page's template that retrieves a cookie and does the redirect in case. To set a cookie you'd need to make a custom plugin or make use of Lewis Jenkins' <a href="https://github.com/lewisjenkins/craft-lj-cookies/" rel="nofollow">"LJ-Cookies plugin"</a>.</p>

<p>For your per-city stories index page you would make your new-york/ template list the city specific stories by adding the category relation / the city parent entry as a criteria to the ElementCriteriaModel.</p>

<p>If you choose to use categories, <a href="http://buildwithcraft.com/docs/categories" rel="nofollow">see here</a> how to set up a category template (tick "Categories in this group have their own URLs"). Additionally you probably want to make your categories field required (so that you have to select a city) and set the limit to 1 (max. allowed cities) in the field settings.</p>

<p>For the structure solution you may want to read through <a href="http://buildwithcraft.com/help/entry-type-templates" rel="nofollow">this article</a> in the docs, to learn how to give entry types individual templates.</p>

CC BY-SA 3.0
2015-01-21T08:42:11.687Z
I'm sorry if this answer is a little confusing, I probably shouldn't have mixed up the two suggested solutions (categories / structure), but have written them one after the other.
carlcs
CC BY-SA 3.0
2015-02-02T02:26:40.427Z
For the entry, is there a way to automatically select the city based on the users location? So staff in New York would add content and it'll show up, just removing that step? I have not done much admin customisation except for extra columns and a dashboard.
Ryan
CC BY-SA 3.0
2015-01-20T22:47:55.783Z
2015-01-21T08:35:20.873Z
2015-01-21T08:35:20.873Z
carlcs
<p>Categories is definitely the right idea here! In fact, I'd recommend creating a <strong>Category URL Format</strong>, and using that to represent each city:</p>

<p><img src="https://i.sstatic.net/IwioO.jpg" alt="enter image description here"></p>

<hr>

<p>Within your "_city-category" template, you would list all entries <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="nofollow noreferrer">related to</a> that particular category (aka: city). The <code>slug</code> in this case would be your city name ("new-york", "los-angeles", etc).</p>

<p>The trick here is to think of your categories <strong>first</strong>, and your entries <strong>second</strong>. Instead of having "entries with categories", you are creating <strong>"categories which have entries"</strong>. Make your categories the main attraction.</p>

<p>As <a href="https://craftcms.stackexchange.com/a/6703/45">carlcs said in his answer</a>, you'll probably want to use the <a href="https://github.com/lewisjenkins/craft-lj-cookies/" rel="nofollow noreferrer">LJ Cookies plugin</a> to remember which city each visitor has selected. It's pretty easy to generate a list of cities (aka: list of categories), and each city would link to its respective dynamic category page.</p>

CC BY-SA 3.0
2015-01-21T08:20:23.437Z
What do you mean with "categories which have entries"? How and where would I add a new entry to such a category without relating it manually?
carlcs
CC BY-SA 3.0
2015-01-21T08:24:43.413Z
All I mean is a change in perspective. You'll still want to attach a **category field** to your entries, so you can select which city each entry belongs in. And the [`relatedTo`](http://buildwithcraft.com/docs/relations#the-relatedTo-param) parameter works both ways... so you can easily find entries which contain a given category, **from the dynamic category page**.
Lindsey D
CC BY-SA 3.0
2015-01-21T08:27:50.953Z
To clear up any confusion... It is technically possible to attach fields directly to your categories (and thus could create an "Entries" field), but that's not what I'm advocating. I'm still recommending the inverse... that you create a "Categories" field, and attach it to your section. Choose the category (city) of each entry from the entry side.
Lindsey D
CC BY-SA 3.0
2015-01-21T07:48:43.507Z
2015-01-21T07:48:43.507Z
2017-04-13T12:50:59.930Z
Lindsey D
<p>Just have a question on the way way to approach this: I have entire sections of a website that are per city and the number of cities might grow. I plan to create a city category and add the cities to it, then those cities appear when people add content. Then in the front-end, get those categories into a list and show them so a user can select them. Once selected it would write it to a cookie or localStorage.</p>

<p>Can I organise my content via url like /newyork/ and it would pick up the URL and fetch that data? Or when I visit the site and I have localStorage set, it would redirect me to /mychosencity/ ?</p>

<p>Just trying for the fastest, most efficient way both in terms of redirecting, but also implementing per city content on a whole site as well as in the templates.</p>

CC BY-SA 3.0
2015-01-20T09:56:12.203Z
perhaps a case for locales?
Marion Newlevant
CC BY-SA 3.0
2015-01-20T23:13:55.553Z
Interesting idea to mis-use the localization feature for this. It'd allow you to have city specific sub-domains newyork.mysite.com and to "translate" stories for multiple cities. Probably not the best user experience in the backend if you (almost) never have the same content for multiple cities / languages, but I'm not totally sure about that. Maybe it plays out nicely?
carlcs
CC BY-SA 3.0
2015-01-21T07:22:58.263Z
This actually sounds like a bad use-case for locales, and a great use-case for categories. In terms of maintenance, it's **far** easier to add new categories rather than locales. And the likelihood of each city needing translated fields is slim.
Lindsey D
<entries>
Show content based on chosen city
# Show content based on chosen city

(Asked by: username_0 on 2015-01-20)

Just have a question on the way way to approach this: I have entire sections of a website that are per city and the number of cities might grow. I plan to create a city category and add the cities to it, then those cities appear when people add content. Then in the front-end, get those categories into a list and show them so a user can select them. Once selected it would write it to a cookie or localStorage.

Can I organise my content via url like /newyork/ and it would pick up the URL and fetch that data? Or when I visit the site and I have localStorage set, it would redirect me to /mychosencity/ ?

Just trying for the fastest, most efficient way both in terms of redirecting, but also implementing per city content on a whole site as well as in the templates.

**Tags:** entries

> **Comments:**
> **username_1**: perhaps a case for locales?  
> **username_2**: Interesting idea to mis-use the localization feature for this. It'd allow you to have city specific sub-domains newyork.mysite.com and to "translate" stories for multiple cities. Probably not the best user experience in the backend if you (almost) never have the same content for multiple cities / languages, but I'm not totally sure about that. Maybe it plays out nicely?  
> **username_3**: This actually sounds like a bad use-case for locales, and a great use-case for categories. In terms of maintenance, it's **far** easier to add new categories rather than locales. And the likelihood of each city needing translated fields is slim.  

---

## 2 answers

### Answer by username_2 on 2015-01-20. Score: 3

Yes, you can totally do this with categories, but then you have to assign a city category to each entry manually. Another idea would be to use a structure section for all of your entries, with top level entries being entries for the cities and child entries for the stories (you can make use of different entry types).

Your cities index page to promt the user for their preferred city would list the categories or the top level entries of your structure. Use a conditional in this page's template that retrieves a cookie and does the redirect in case. To set a cookie you'd need to make a custom plugin or make use of Lewis Jenkins' ["LJ-Cookies plugin"](https://github.com/lewisjenkins/craft-lj-cookies/).

For your per-city stories index page you would make your new-york/ template list the city specific stories by adding the category relation / the city parent entry as a criteria to the ElementCriteriaModel.

If you choose to use categories, [see here](http://buildwithcraft.com/docs/categories) how to set up a category template (tick "Categories in this group have their own URLs"). Additionally you probably want to make your categories field required (so that you have to select a city) and set the limit to 1 (max. allowed cities) in the field settings.

For the structure solution you may want to read through [this article](http://buildwithcraft.com/help/entry-type-templates) in the docs, to learn how to give entry types individual templates.

> **Comments:**
> **username_2**: I'm sorry if this answer is a little confusing, I probably shouldn't have mixed up the two suggested solutions (categories / structure), but have written them one after the other.  
> **username_0**: For the entry, is there a way to automatically select the city based on the users location? So staff in New York would add content and it'll show up, just removing that step? I have not done much admin customisation except for extra columns and a dashboard.  

---

### Answer by username_3 on 2015-01-21. Score: 2

Categories is definitely the right idea here! In fact, I'd recommend creating a **Category URL Format**, and using that to represent each city:

![enter image description here](https://i.sstatic.net/IwioO.jpg)

---

Within your "\_city-category" template, you would list all entries [related to](http://buildwithcraft.com/docs/relations#the-relatedTo-param) that particular category (aka: city). The `slug` in this case would be your city name ("new-york", "los-angeles", etc).

The trick here is to think of your categories **first**, and your entries **second**. Instead of having "entries with categories", you are creating **"categories which have entries"**. Make your categories the main attraction.

As [username_2 said in his answer](https://craftcms.stackexchange.com/a/6703/45), you'll probably want to use the [LJ Cookies plugin](https://github.com/lewisjenkins/craft-lj-cookies/) to remember which city each visitor has selected. It's pretty easy to generate a list of cities (aka: list of categories), and each city would link to its respective dynamic category page.

> **Comments:**
> **username_2**: What do you mean with "categories which have entries"? How and where would I add a new entry to such a category without relating it manually?  
> **username_3**: All I mean is a change in perspective. You'll still want to attach a **category field** to your entries, so you can select which city each entry belongs in. And the [`relatedTo`](http://buildwithcraft.com/docs/relations#the-relatedTo-param) parameter works both ways... so you can easily find entries which contain a given category, **from the dynamic category page**.  
> **username_3**: To clear up any confusion... It is technically possible to attach fields directly to your categories (and thus could create an "Entries" field), but that's not what I'm advocating. I'm still recommending the inverse... that you create a "Categories" field, and attach it to your section. Choose the category (city) of each entry from the entry side.  

---

