Support » Developing with WordPress » Region category post

  • Resolved Taro


    I would like to display the correct menu and sidebar for a “region” post.

    The following post is displayed in the region benelux. It’s currently showing the basic menu and basic sidebar. The menu thats should be displayed is the menu name ‘Benelux’ and the sidebar should be ‘Benelux’.

    The current categories active for this post is Benelux-News and ‘brand name‘. A custom taxonomie is also active for this post called Location and Benelux is active.

    The permalink “should” be as following’brand name’/’post name’

    The following code(code here) is already in the function.php created by @bcworkz.

    The page I need help with: [log in to see the link]

Viewing 11 replies - 16 through 26 (of 26 total)
  • I will change the redirects 301 when the fallback is ready.

    I’m unsure how to answer your question, I’m not sure what you mean by “if the posts are located within the permalink structure”. Do you mean if the request is for a single post?

    I was just looking for a other option to use. The taxonomy location is something that has to be changes menual per post. Is it possible to use the standard category “Benelux-News” (International-News) this category is always active for every post. I have done this to output the news items on the news page(s).

    Moderator bcworkz


    Almost anything is possible 🙂

    Are you suggesting that if a post’s category is Benelux-News, we set the $region global and thus the first permalink term as “benelux”? And similar for International-News? Set $region and the permalink term as “international”?

    If so, then yes, it’s possible. It sounds like a good plan. Are the category term slugs just lower case versions? benelux-news and international-news? Please also determine the term IDs for these. If you go to edit a term by picking Edit from the category list table accessed under the Posts admin menu, the URL in the address bar will have an URL parameter “tag_ID” that equals some integer. That integer is the term ID. “tag_ID” is used for all taxonomy term IDs, not just for tags. ¯\_(ツ)_/¯

    Moderator bcworkz


    Below are the revised callbacks for pre_get_posts and post_link with fallback code in place. Replace what you currently have with these versions. In the tg_add_region() callback, I’ve assumed the IDs for the Benelux-News and International-News categories are 123 and 124 respectively. You must use the actual IDs in each line (that begins with if ( in_array(...) so that the proper location term can be assigned.

    // Insert region into post links
    add_filter('post_link', 'tg_add_region', 999, 3 );
    function tg_add_region( $link, $post, $leavename) {
       global $region;
       $cats = wp_get_post_categories( $post->ID, array('fields'=>'ids',));
       if ( in_array( 123, $cats )) $region = 'benelux';
       if ( in_array( 124, $cats )) $region = 'international';
       if ( null == $region ) $region = 'international';
       $link = str_replace('.com/', ".com/$region/", $link );
       return $link;
    // Add region category to main queries
    add_action('pre_get_posts', 'tg_set_region');
    function tg_set_region( $query ) {
      if ( get_option('page_on_front') == $query->get('page_id')) return;
      if ( !is_admin() && $query->is_main_query()) {
        global $region;
        $explode = explode('/', $_SERVER['REQUEST_URI']);
        $cat = $query->get('category_name');
        $region = $explode[1];
        if ( ! in_array( $region, array('benelux','international',))) $region = 'international';
        if ( '' != $cat ) $cat .= '+' . $region;
            else $cat = $region;
        $query->set('category_name', $cat);

    When inserting the region in post links, we try to use the assigned category to decide upon the region. In both the post_link and pre_get_posts callbacks, when all else fails in assigning the correct region, we use “international”.

    changed 123 to 230 and 124 to 231. But the code did not work correctly. It gave me a blank homepage(and all other pages). Changed back to old code > attached function.php with new code.

    Moderator bcworkz


    Were any errors logged with that blank page? The code worked on my installation, which of course does not match yours, so compatibility issues have to be expected. If we can find out where the conflict is, the fall backs will work.

    When i use the following code

    Its just giving me a blank page. When i look at the page code the following is showing only:


    i can’t see any errors

    • This reply was modified 1 year, 11 months ago by Taro.
    • This reply was modified 1 year, 11 months ago by Taro.

    I did add the debug on and the following is showing:

    Fatal error: Cannot redeclare tg_add_region() (previously declared in in on line 552

    The code is now working i needed to remove the following codes to get it working:

    The correct permalink (menu etc) is still not working correctly when you for example search on google for the following “weisz group Edox – Fleet 1650” then you get the international layout (menu, sidebar, permalink). But this should be Benelux.

    When i edit this post the permalink shows “international” and should be Benelux. The Primary categorie is “Edox” and subcategorie is “Benelux-news”.

    And the custom taxanomy is “Benelux”

    When you follow the news page on the website(inside benelux) you will find the same post in the correct location. But this is because the code finds the selected benelux menu and sidebar. This works correct.

    • This reply was modified 1 year, 11 months ago by Taro.
    • This reply was modified 1 year, 11 months ago by Taro.
    Moderator bcworkz


    Hi Taro,

    Sorry for a slow reply. I sometimes have trouble with notifications. Were you able to solve this? When I follow the link in Google results titled “Edox – Fleet 1650 – WEISZ GROUP”, it takes me to benelux/benelux-news/edox-fleet-1650/ and the Benelux sidebar appears. I’m not sure what the correct menu looks like. The one that appears is the same as for the /benelux/ page so I’m assuming it is correct.

    In other words, everything now looks OK to me. If the problem was only when the Google link was followed, the link was probably formed incorrectly and maybe it has since been corrected?

    Hi Bcworkz,

    The code is working correct. I did not have problems with it after google updated the links.

    Currently we are looking to change some design elements maybe a new theme but i’m pretty sure i can set all the code to the new layout.

    I hope this will be all the time that i stole from you! Thanks for all the work!

Viewing 11 replies - 16 through 26 (of 26 total)
  • The topic ‘Region category post’ is closed to new replies.