3

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.

3
  • perhaps a case for locales? Jan 20 '15 at 9:56
  • 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
    Jan 20 '15 at 23:13
  • 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
    Jan 21 '15 at 7:22
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".

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 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 in the docs, to learn how to give entry types individual templates.

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.
    – carlcs
    Jan 21 '15 at 8:42
  • 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
    Feb 2 '15 at 2:26
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


Within your "_city-category" template, you would list all entries related to 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 carlcs said in his answer, you'll probably want to use the LJ Cookies plugin 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.

3
  • 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
    Jan 21 '15 at 8:20
  • 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 parameter works both ways... so you can easily find entries which contain a given category, from the dynamic category page.
    – Lindsey D
    Jan 21 '15 at 8:24
  • 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
    Jan 21 '15 at 8:27

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.