• 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

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter ad8n-2

    (@ad8n-2)

    Would I be right in thinking that the problem is most likely in the functions.php code, rather than the city.php template? Because the menus must be processed before the body code is.
    I’m not sure why the second block of code didn’t display properly in the previous post, so here it is again!

    // 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();

    Thread Starter ad8n-2

    (@ad8n-2)

    Today I tried removing the functions code to see if the menus would display properly without it. Although I expected the loop to fail, I was hoping that I could at least isolate what code is causing the problem.
    Unfortunately, simply removing the functions code made no difference to the menus. (And yes, I did resave the permalinks).
    So does this mean that there’s a problem with the way I’m calling the category page?
    Anybody have any suggestions?

    Thread Starter ad8n-2

    (@ad8n-2)

    Latest update!
    It goes without saying that I can hardcode the menus into the header. This seems pointless to me, so I’m trying to avoid it.
    However, if I go to one of the pages where the menu is not displaying, the page title is not displaying in the browser header/tab either. For instance, my ABOUT page should read: ‘About | GA Airport Report’.
    Instead it reads: ‘ | GA Airport Report’.
    What gives?
    Thanks,
    A.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Menus disappear when using rewrite rules’ is closed to new replies.