Menus disappear when using rewrite rules
-
I have a airport services website that uses the Arras theme (www.gaairportreport.com). I recently added some code so that I could pass variables through the URL, so that I could write a link such as: “gaairportreport.com/city/los-angeles/” and list all of the posts I had marked with the “los-angeles” category.
I added the following code to the functions.php file in my child theme:
// CITY: Tell WordPress to keep track of our CITY query variables (edit "category" names) function add_query_vars($aVars) { if ( $query_vars !='nav_menu_item') { $aVars[] = "loc_city"; // represents the name of the location city as shown in the URL return $aVars; } } // CITY: hook add_query_vars function into query_vars add_filter('query_vars', 'add_query_vars'); // CITY: Tell WordPress to use the last portion of the URL to populate the new query variable (using custom rewrite rule) function add_rewrite_rules($aRules) { $aNewRules = array('city/([^/]+)/?$' => 'index.php?pagename=city&loc_city=$matches[1]', 'city/([^/]+)/([^/]+)/?$' => 'index.php?pagename=city-cat&loc_city=$matches[1]&loc_cat=$matches[2]'); // $aNewRules = array('city/([^/]+)/?$' => 'index.php?pagename=city&loc_city=$matches[1]'); $aRules = $aNewRules + $aRules; return $aRules; } // CITY: hook add_rewrite_rules function into rewrite_rules_array add_filter('rewrite_rules_array', 'add_rewrite_rules');
I then created a template (city.php) into which I put the following code
`// Identify and decode query variable into Category Slug
if(isset($wp_query->query_vars[‘loc_city’])) {
$sLocCity = urldecode($wp_query->query_vars[‘loc_city’]);
}// Create new variable and query while passing slug using WP ‘category_name’ parameter
$myQuery = new WP_Query();
$myQuery->query( ‘category_name=’ . $sLocCity );// WP Loop continues from here
if ($myQuery->have_posts()) : while($myQuery->have_posts()) : $myQuery->the_post();’Obviously I then created a new page called city, that used the city.php template.
Now when I go to the site the code works as it should filtering the posts as required, but on the pages that use the code my menus disappear (and they are not working if I click through a link to see an individual post – so I’m clearly not using the default single post template for the individual posts either. The menus still work on the home and regular category pages.
I’ve spent the last week (and all of Thanksgiving weekend!) trying to find solutions, but most of the ones for custom post types don’t work for me. I’m a WP newbie, so forgive me if I’m missing something obvious!
Thanks,
Antoni
- The topic ‘Menus disappear when using rewrite rules’ is closed to new replies.