Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a website where we have a menu in the sidebar that pulls the list of the parent for the current page, and all the children of that page.

The problem is that, on pages where there's a query for posts included, this menu doesn't work. I got around it by creating a custom menu for any pages like this, but then the current_page_item class does not appear. How can I fix this code to work on these odd pages? Do I need to adjust the post queries within the theme pages, or the sidebar query for the pages?

This is the normal menu query:

if($post->post_parent) {
  $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&depth=1");
  $titlenamer = get_the_title($post->post_parent);
  }

  else {
  $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1");
  $titlenamer = get_the_title($post->ID);
  }
  if ($children) { ?>

  <h6> <?php echo $titlenamer; ?> </h6>
  <ul>
  <?php echo $children; ?>
  </ul>

And this is the adjusted code that I tried using that does not display the current_page_item class in:

  if($post->post_parent) {
  $children = wp_list_pages("title_li=&child_of=11&echo=0&depth=1");
  $titlenamer = get_the_title($post->post_parent);
  }

  else {
  $children = wp_list_pages("title_li=&child_of=11&echo=0&depth=1");
  $titlenamer = get_the_title(11);
  }
  if ($children) { ?>

  <h6> <?php echo $titlenamer; ?> </h6>
  <ul>
  <?php echo $children; ?>
  </ul>

Thanks for any and all help.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

It sounds like you just need wp_reset_postdata() after you run any extra queries, and before you output your menu.

share|improve this answer
    
It's crazy. I had tried that before, but I never got it to work. Then I realized that I should have tried wp_reset_query(), and of course that worked because it's using query_posts. Maybe I'm pushing my luck now, but I should ask - for story post pages, where I still want that sidebar menu to appear, is there a way of still having that current_page_item set for the one page item? Of even just telling it, like in the second code example above which parent and then which child to use as current? –  Andrew Jan 10 at 5:15
    
wp_reset_query also itself calls wp_reset_postdata, but for extra queries you should be using WP_Query and not query_posts. See this question for info on why. –  Milo Jan 10 at 5:19
    
I'm not sure what you mean by "story post pages"? –  Milo Jan 10 at 5:20
    
Sorry. So, the page where I had this issue had a query for posts. When the user clicks through, they're going to the single template, which means I can't use the same sidebar there, but I want the same menu to appear there without hardcoding it in. Does that help? –  Andrew Jan 10 at 5:22
    
how would you know what page the user came from to be able to display the correct menu? you'd have to somehow pass that information from the originating page to the target page, like maybe appending something on the URL like ?source=n, where n is the ID of the page you came from. you'd then have to check if $_GET['source'] is set and show the appropriate menu that way. but then you'd have to output the menu manually to set the proper classes. a somewhat involved process! –  Milo Jan 10 at 5:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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