tags:

views:

85

answers:

1

Because I'm using a couple of custom post types and two languages I decided not to use WP's own menu function, and used these conditional statements instead -

<div class="navigation">
<ul>
    <li><a <?php if(is_page('home') || is_page('hem')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo(url) . '">Home'; } else { echo 'href="' . get_bloginfo(url) . '">Hem'; } ?></a></li>
    <li><a <?php if(is_page('offers') || is_page('erbjudanden') || in_category('offers') || in_category('erbjudanden')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'offers">Offers'; } else { echo 'href="' . get_bloginfo('url') . '/erbjudanden">Erbjudanden'; } ?></a></li>
    <li><a <?php if(is_page('properties-spain') || is_page('fastigheter-spanien')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'properties-spain">Properties in Spain'; } else { echo 'href="' . get_bloginfo('url') . '/fastigheter-spanien">Fastigheter i Spanien'; } ?></a></li>
    <li><a <?php if(is_page('properties-usa') || is_page('fastigheter-usa')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'properties-usa">Properties in USA'; } else { echo 'href="' . get_bloginfo('url') . '/fastigheter-usa">Fastigheter i USA'; } ?></a></li>
    <li><a <?php if(is_subpage('information') || is_subpage('radgivning')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'information/process-of-purchase">Information'; } else { echo 'href="' . get_bloginfo('url') . '/radgivning/hur-ett-kop-gar-till">Rådgivning'; } ?></a></li>
    <li><a <?php if(is_page('about-us') || is_page('om-oss')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'about-us">About us'; } else { echo 'href="' . get_bloginfo('url') . '/om-oss">Om oss'; } ?></a></li>
    <li class="last"><a <?php if(is_page('contact') || is_page('kontakt')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'contact">Contact'; } else { echo 'href="' . get_bloginfo('url') . '/kontakt">Kontakt'; } ?></a></li>
</ul>
</div>

Now this works excellent including it in the header file but in the footer file only some of the links are given a class of active when on the actual page. All the items in the menu exists as pages, with one version for each language (swedish and english). This error just seems really weird and I've tried checking with different conditional parameters such as page title, page id etc. with no results. Any ideas?

+1  A: 

Sometimes queries higher up in the template source will corrupt the output of conditional tags lower down (i.e. in your footer). The trick is to put wp_reset_query() before you use them in your footer include:

    <?php wp_reset_query(); ?>
    <div class="navigation">
    <ul>
        <li><a <?php if(is_page('home') || is_page('hem')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo(url) . '">Home'; } else { echo 'href="' . get_bloginfo(url) . '">Hem'; } ?></a></li>
        <li><a <?php if(is_page('offers') || is_page('erbjudanden') || in_category('offers') || in_category('erbjudanden')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'offers">Offers'; } else { echo 'href="' . get_bloginfo('url') . '/erbjudanden">Erbjudanden'; } ?></a></li>
        <li><a <?php if(is_page('properties-spain') || is_page('fastigheter-spanien')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'properties-spain">Properties in Spain'; } else { echo 'href="' . get_bloginfo('url') . '/fastigheter-spanien">Fastigheter i Spanien'; } ?></a></li>
        <li><a <?php if(is_page('properties-usa') || is_page('fastigheter-usa')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'properties-usa">Properties in USA'; } else { echo 'href="' . get_bloginfo('url') . '/fastigheter-usa">Fastigheter i USA'; } ?></a></li>
        <li><a <?php if(is_subpage('information') || is_subpage('radgivning')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'information/process-of-purchase">Information'; } else { echo 'href="' . get_bloginfo('url') . '/radgivning/hur-ett-kop-gar-till">Rådgivning'; } ?></a></li>
        <li><a <?php if(is_page('about-us') || is_page('om-oss')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'about-us">About us'; } else { echo 'href="' . get_bloginfo('url') . '/om-oss">Om oss'; } ?></a></li>
        <li class="last"><a <?php if(is_page('contact') || is_page('kontakt')) { echo 'class="active" '; } if (ICL_LANGUAGE_CODE == 'en') { echo 'href="' . get_bloginfo('url') . 'contact">Contact'; } else { echo 'href="' . get_bloginfo('url') . '/kontakt">Kontakt'; } ?></a></li>
    </ul>
    </div>
Pat
I see. I tried using wp_reset_query() but it gave no effect. Could it have something to do with the fact that I'm using wp_query instead of the core loop throughout the page? Perhaps wp_query has another reset call?
Staffan Estberg
Interesting, that function has always worked for me. Perhaps if you're using PHP5 you could clone your $wp_query object in your header and then just before your footer restore it from the clone, which would put everything back in its original state: http://www.wprecipes.com/wordpress-tip-storing-wp_query-for-future-use
Pat
Sorry, didn't see your reply until now. I'm not sure what you mean by restoring the wp_query_object since I don't create a query for the actual menu items. The queries on the page are only used when fetching posts/page content.
Staffan Estberg
I tried creating a menu from WP's core functions and it still doesn't show an active link! Now I'm confused. While some of them show, and everyone shows in the header, three of them doesn't get an active state: properties-spain, properties-usa and about-us.
Staffan Estberg
The reason you might want to try cloning and restoring the $wp_query object is because other queries in your theme (or WordPress function calls behind the scenes) could be altering the state of your $wp_query object and causing the conditional functions like is_page() to stop working. If you still can't get it working, and it always works for your header menu, what if you captured the output of your menu in a variable and then just echoed the same variable in the header and footer.
Pat
I see. I did a similar solution to what you suggested, created a global variable in the header file which fetches the ID of the page and then create conditional statements in the footer based on that value. Thanks for taking me in the right direction :)
Staffan Estberg