Region category post
-
I would like to display the correct menu and sidebar for a “region” post.
Example:
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 domain.com/benelux/news/’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]
-
Hi Taro, ready to tackle this again eh? ??
From your description, we are on to a slightly different issue than what we were addressing in the past. If I am understanding correctly, you need to insert the region name into all current post permalinks. If the region name is still in the global $region as before, the following added to functions.php should work. As before, I’ve no way to test this. If it fails to work as expected, let me know what URL it did generate and what it should be. As before, you can disable the code by commenting out the add_filter() line.
I’m assuming no other code is inserting the region into post links. If there is, which code is applied where needs to be coordinated somehow.
// Insert region into post links add_filter('post_link', 'tg_add_region', 999, 3 ); function tg_add_region( $link, $post, $leavename) { global $region; $link = str_replace('.com/', ".com/$region/", $link ); return $link; }
This works great! The permalink and menu are correct. But the sidebar is still incorrect.
This is the current post page code.- This reply was modified 7 years, 2 months ago by Taro.
That’s great news! Good progress then. The sidebar template that’s attempted to be loaded in all cases is currently sidebar-benelux.php. If that does not exist, sidebar.php is used. If you are not getting the Benelux sidebar, it’s maybe either misnamed or its permissions prevent WP from using it. Or is the problem the other way around, that the normal sidebar never loads when the request is not for Benelux? I have the fix either way as long as the proper files exist and are accessible.
To get sidebar-benelux.php to only load when $region == ‘benelux’ and to otherwise load sidebar.php, first be sure
global $region;
occurs on the page template somewhere as active code. It’s currently commented out in at least two places. You could alter line 30 like so:
<?php global $region; /* global $wp_query;
If every possible region has its own sidebar template (each named following the format sidebar-{$region}.php ), you can change the get_sidebar() call on line 62 to this:
<?php get_sidebar( $region ); ?>
If the filename following the format sidebar-{$region}.php does not exist or is inaccessible, sidebar.php will be used.
Changed the code but sidebar is not loading.
Other problem is that my slider is not loading.
I’m seeing a sidebar with the Benelux map at the top of the benelux/news/ page. So it appears you solved the sidebar issue? Or is that the wrong sidebar?
The Benelux slider is coded to only appear when is_front_page() is true. I don’t think this is ever the case for Benelux pages. Try removing
is_front_page() &&
from line 2 of the slider function. This might result in the slider appearing where it shouldn’t, but the change will confirm this was preventing the slider from working.Assuming that change gets the slider to work too many pages, what is the criteria for where the slider should appear? Only on the news page? Several pages? All Benelux pages? Unless all, please determine the IDs of all pages where the Benelux slider should appear. We can code the slider to only appear on those IDs. Or is there some other criteria to determine when the slider appears?
The slider is fixed. The sidebar is correct on all pages but not on the post pages example.
Getting closer to a complete solution! That page should be loading sidebar-worldwide.php, but is loading just sidebar.php. Is that correct? Have you posted the code for the single post page template that has the
get_sidebar( $region );
line yet? Please link to it. Sorry if you already have, I’m not sure which link to follow. I’m mainly looking for not only the above line, butglobal $region;
as well. If those lines exist, and the sidebar template sidebar-worldwide.php exists in the theme folder, something odd is happening. Maybe when I examine the template code something will get my attention. Please post the sidebar-worldwide.php code as well, maybe the problem is in this file.First note; i changed World Wide to International.
I got a single-benelux.php and a single-international.php where the
get_sidebar( $region );
is now active. I did not add the single.php to the child theme. Should i remove both pages and add the single.php to the child and change the region there?sidebar-benelux.php should be loading if the catagory “benelux-news” is active. The sidebar is working correct! (i could probably remove this category because it’s using the region code)
sidebar-international.php should be loading if the catagory “international-news” is active. The sidebar is working correct! (i could probably remove this category because it’s using the region code)
I think we are almost there. Soon going to check some other pages and functions. And maybe try to activate the search function for the website.
The template where the sidebar does not work is the one I’m concerned about. I guessed it is single.php. Whatever template is used should be copied to the child folder if not already there. Add
global $region;
to any template having sidebar issues and change the sidebar call to `get_sidebar( $region );Changing the category name is fine, but the sidebar template name needs to match what ever value is used by $region. From your link in your post before last it appears to still be worldwide, so the sidebar template name should be sidebar-worldwide.php regardless of the actual category name.
OTOH, we could reconfigure the $region assignment to use international as the operative term instead. You would need to change your permalinks to contain /international/ instead of /worldwide/ and then sidebar-international.php file would be a valid template name. I’m not sure how $region picks up the worldwide term to start with. In the post_link code I recently gave you in this topic that inserts the $region value into the links, $region is already defined or the code wouldn’t work. In my earlier pre_get_posts code (for functions.php) that assigns a value to $region gets the value from the requested permalink.
I think it must be the main sidebar where one can pick Worldwide or Benelux where the term is first established. Then my code perpetuates which ever is picked. You’d need to change this template code to utilize international instead of worldwide and I think then international would be used throughout — in URLs, in the sidebar template name, everywhere. The category “international” will then be what is queried. My pre_get_posts code sets the requested category name for the query to whatever is passed in the URL. Since the last link you provided still has /worldwide/ in it, apparently the category term worldwide still exists, it wasn’t changed.
Perhaps you only changed the category label to International? The slug remains worldwide? You can manage this however you want. What that is needs to be reflected in the initial sidebar and anywhere else the users picks International or Benelux.
Not sure what’s going wrong but the post permalink is stated as: https://weiszgroup.com//danish-design/danish-design-new-jewellery-2/
If you add “international” at the /international/ the sidebar and menu is working correct. But you can add whatever you like between the //
Example: https://weiszgroup.com/letsaddsometexthere/danish-design/danish-design-new-jewellery-2/i did add a redirec 301 from /worldwide to /international
- This reply was modified 7 years, 1 month ago by Taro.
The no region permalink is a result of $region not being defined. We have two things to address. One is to do whatever is needed to ensure $region is always defined. The other is to have a fallback should it not be defined anyway. The fallback is easy, but what would you prefer? No region (and no double slash), international, or benelux?
No region can be a problem with the pre_get_posts code because it pulls the $region assignment from the requested URL. As it stands, no region will assign something like “danish-design” as $region and things will not work right. Again, there should be a fallback in the pre_get_posts code should the value it pulls from the URL not be either international or benelux. In this case no region is not an option. One or the other should be always be assigned.
That will take care of cases where $region is not defined, but it doesn’t address why there is no region assigned in the first place. What is your vision for the site regarding regions? It appears to me only the home page should have $region undefined, once a user picks one or the other, the selected region should always be assigned to the $region variable one way or another.
Speaking of home page, how is that configured in the WP reading settings? Does it remain “latest posts” where the home page content is handled through template changes? Or is it set to a “static page”?
What is your plan for possibly adding more regions in the future? From way back when we first talked of this, I think I remember it’s a possibility for the distant future. I ask because in order to have fallback values, we need to establish what are acceptable values right now. Thus if another region is to be added, it will need to be added to this fallback code as well as by adding another category.
It seems to me that if one starts at the home page and selects a region, that everything works correctly. But I’m not familiar with your site. Other than typing the wrong URL, how would one get to where things do not work right, where the region disappears from links? I need to know how a visitor could cause the problem in order to have any hope of fixing it.
The no region permalink is a result of $region not being defined. We have two things to address. One is to do whatever is needed to ensure $region is always defined. The other is to have a fallback should it not be defined anyway. The fallback is easy, but what would you prefer? No region (and no double slash), international, or benelux?
No region can be a problem with the pre_get_posts code because it pulls the $region assignment from the requested URL. As it stands, no region will assign something like “danish-design” as $region and things will not work right. Again, there should be a fallback in the pre_get_posts code should the value it pulls from the URL not be either international or benelux. In this case no region is not an option. One or the other should be always be assigned.
That will take care of cases where $region is not defined, but it doesn’t address why there is no region assigned in the first place. What is your vision for the site regarding regions? It appears to me only the home page should have $region undefined, once a user picks one or the other, the selected region should always be assigned to the $region variable one way or another.
I think the best way is to use my custom taxonomy. The taxonomy is called location. The posts and pages both have this taxonomy and are devided by “Benelux” and “International”
Speaking of home page, how is that configured in the WP reading settings? Does it remain “latest posts” where the home page content is handled through template changes? Or is it set to a “static page”?
The normal home page is currently the static intro page. Posts page is not selected.
What is your plan for possibly adding more regions in the future? From way back when we first talked of this, I think I remember it’s a possibility for the distant future. I ask because in order to have fallback values, we need to establish what are acceptable values right now. Thus if another region is to be added, it will need to be added to this fallback code as well as by adding another category.
The base will alweays stay “Benelux” or “International”.
It seems to me that if one starts at the home page and selects a region, that everything works correctly. But I’m not familiar with your site. Other than typing the wrong URL, how would one get to where things do not work right, where the region disappears from links? I need to know how a visitor could cause the problem in order to have any hope of fixing it.
I think this is only visable when looking for example on google. Search Engines will search for all links.
Yes, using your location taxonomy is fine. The issue is trying to determine the correct term within that taxonomy to use when generating links and calling special sidebars and menus, etc. There is not always a convenient object from which to get the assigned term, it often needs to carry over from the previous request.
I think you are saying the site is working correctly for new visitors, or when proper URLs are used, the problem is bad backlinks out “in the wild” that perhaps used to be proper but no longer fit the current URL scheme. In that case, I recommend adding .htaccess rules (assuming Apache server) that rewrite the bad requests that come in from the wild to one that fits the current scheme, sending a 301 redirect header to signal that the request is no longer valid and should be updated. The new URL will then inherit any page rank the previous URL had accumulated. It’s very inefficient to attempt to manage such backlinks within WP itself. .htaccess rules are much better.
This means you should compile a list of URLs that are coming in causing issues and what corresponding proper URL should be redirected to. There is likely a pattern where a formulated response is easily composed.
The permalink from the pages are correct and don’t need the custom taxonomy. The pages use the structure from the menu.
It would be beter if the posts would use something like the menu structure. Is there a option to scan the menu structure and automatic add “Benelux” of “International” if the posts are located within the permalink structure?
image example current path- This reply was modified 7 years, 1 month ago by Taro.
That is an invalid link because the global $region is undefined. I suppose this is due to an incoming link that failed to conform to the new permastruct. As I mentioned, these are best dealt with with .htaccess redirects. But as a fallback, if these problem links in all cases have the correct location taxonomy term assigned to the queried object (typically a single post), then we can enhance the location term insertion code to determine that term and use it if $region is undefined.
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? And if so, do you wish to add the appropriate location/region menu item to whatever menu is used? In the case of single post requests, yes, but if the $region data and location term insertion were handled properly, wouldn’t the menu already have the proper menu item?
I think it’s wise to develop a fall back method of determining the $region value for single post requests that currently are missing the location part of the permalink. I think that will solve the issue you are trying to resolve by inserting a menu item, if my assumptions in the previous paragraph are correct. I’m pretty busy right now, but could find the time early next week if you think this is a good idea.
- The topic ‘Region category post’ is closed to new replies.