City Filter (6 posts)

  1. flbestdeal
    Posted 1 year ago #


    I have a site with around 25 articles. What I want to do now, is filter the articles and write new one by related by cities.

    What I want is click over New York for example and every article will be about New York, but the links of the menu too.

    How can I do that in WP.

    Thanks for any advice,


  2. bcworkz
    Posted 1 year ago #

    You could assign cities as either categories or tags, or create a custom taxonomy if categories and tags already have other purposes. Then the related archive pages will only show related articles.

    To alter the menu to only have related article links, hook into the 'wp_nav_menu_items' and remove all items not of the current category, tag, or term.

  3. flbestdeal
    Posted 1 year ago #

    What I thought to make referents categories and and make the relationship using the tags. Then if I link a tag city it will show everything related to this tag. But how I show the tags and at the same time make the menu linked to the same city. I do not see it clear in your answer.

  4. bcworkz
    Posted 1 year ago #

    You can show the tags by using the Tag Cloud widget. Clicking any tag will lead to an archive page showing all posts with that tag. Manually creating tag page links is easy too. Say you were writing an article about Warsaw and wanted to link to a tag page of the sister city Chicago. The link would look like this: http://example.com/tag/chicago/

    I am assuming your menu is the one defined on the Appearance Menus Screen and your theme displays it using the wp_nav_menu() function. All the items on this menu pass through the 'wp_nav_menu_items' filter. Being a filter, you can write code that adds or removes menu items just before the menu is displayed.

    Your filter code could sort through all the menu items and remove any that do not have the current city tag assigned. Thus the menu only displays links related to the city the page represents.

  5. flbestdeal
    Posted 1 year ago #

    Thank you so much for your answer.

    Maybe you do not get my question, maybe I am missing something.

    I made a drop down menu with 10 cities. Boston, NY, etc. http://www.example.com/tag/boston, http://www.example.com/tag/NY. Every articles is filtered and shows up only thus post related to it city, like Boston or NY.

    But if I go to any other menu they are not related to this city. For example.

    I have one menu as I described above with some cities and some others links. And I have another menu with the mains links like: Nightlife, Restaurants, Travel, etc. If I link over Boston I see only the post related to Boston, but if in that page I make click again over the main links, then it shows up the everything related to this category, but does not filter the active tag or city. And this is what I do not know how to hack.

    Thanks you so much for be there reading what I post and help.

  6. bcworkz
    Posted 1 year ago #

    I think we are both in part not understanding the other!

    The filter I suggested works for menus added and defined through the Edit Menus admin screen.

    It will not work for other menus or any menus not using wp_nave_menu() to display the menu. How to hack those menus would depend on how that menu collects its data and displays it. If you can post the code producing such menus, I might be able to make a suggestion. If the code is more than a dozen lines, please use pastebin.com and provide the link here.

    If I am still misunderstanding, my apologies. A link to a typical city page and a description of what menus need filtering might help.

Topic Closed

This topic has been closed to new replies.

About this Topic