0

My Wordpress site uses a custom front page (home.php). The blog page uses a template (blog.php).

However when I visit the blog page the navigation bar does not highligt the current page. This is because the class .current-menu-item (and the class .current_page_item) are missing from the navigation li. This class is correctly included when I visit all other pages that use the default page template.

Here is the template for the blog page:

<?php
/*
Template Name: Blog
*/

// Which page of the blog are we on?
$paged = get_query_var('paged');
query_posts('cat=-0&paged='.$paged);

// make posts print only the first part with a link to rest of the post.
global $more;
$more = 0;

//load index to show blog
load_template(TEMPLATEPATH . '/index.php');

?>

I've noticed that if I remove the call to query_posts then the nav bar styling works correctly, but the page is left blank.

Any ideas?

2 Answers 2

0

The custom navigation menu uses the global $wp_query object to figure out what page is the current page and should get that class. query_posts() replaces this $wp_query object, so the navigation menu can't apply the correct classes.

Why do you use a separate template for this page? If you specify no template, it should use the home template (home.php or index.php) with the correct posts in the loop, and you should not need your own query_posts().

4
  • Ok, I see you've edited your answer suggesting I don't need a template. You're probably right. In my current setup my front page does not display posts (just a few widgets). I have a separate "news" page which displays the latest blog posts. This is a static page that uses the template blog.php. Is there a more elegant way to achieve the same result?
    – Andy
    Commented May 10, 2011 at 10:58
  • The reason I have is setup like this is because I used some example code I found here
    – Andy
    Commented May 10, 2011 at 11:01
  • @Andy: If you rename your home.php to front-page.php, it will only be used for the static front page, not for the "post index list" at blog. You should then not need a special page template for the "fake" blog page. It will use home.php, and if it does not exist, the standard index.php. It was another question that made me really test this and edit my answer.
    – Jan Fabry
    Commented May 10, 2011 at 11:04
  • Thanks for the info. Everything works as it should, and with a simpler implementation than before.
    – Andy
    Commented May 11, 2011 at 10:31
0

As explained by, Jan query_posts() replaces this $wp_query object.

So what you can do is use WP_Query and store old $wp_query object:

$temp = $wp_query;  
$wp_query = null;
$wp_query = new WP_Query($args); 
while ( $wp_query->have_posts() ) : $wp_query->the_post();
    Do what you need
endwhile;
$wp_query = $temp;

or use a new variable:

$loop = new WP_Query($args); 
while ( $loop->have_posts() ) : $loop->the_post();
    Do what you need
endwhile;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.