1

I have an extra submenu with this code in the sidebar:

if(is_404() || is_search()) :
    echo 'should be empty';
else :
    if($post->post_parent == '68')
        $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    else
        $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    if ($children) { 
        ?><ul><?php echo $children; ?></ul><?php 
    }
endif;

I have a problem with the first line. It's working fine on the 404 page and on the search page if you search for something that doesn't exist, but when you look for something that does exist, the submenu is popping up in the sidebar. What is wrong with this code?

  • Where do you place this code? – Krzysiek Dróżdż Jul 2 '13 at 13:50
  • I place it in sidebar.php – xarai Jul 2 '13 at 14:33
  • Don't hardcode post values. If you really must use 'magic' values, refer to post slugs instead – Tom J Nowell Jun 26 '14 at 23:30
1

If what you want is for the else to show up on 404 pages and on search pages but only when the search returns no results, the conditional you need is ...

if(is_404() || (is_search() && 0 === $wp_query->found_posts)) {
  // ...
} else {
  // ...
}

The difference between that and your code is that I added an additional check to see if the search query has any results. The brackets around is_search() && 0 === $wp_query->found_posts) make the || operate against the whole clause, just like brackets in math, really.

If you don't want the else on any search or 404 pages then you need...

if(!is_404() || !is_search()) {
  // ...
} else {
  // ...
}

... but that is what you have so I am unsure what you are trying to achieve.

  • Hi, what I want is the menu NOT to show up on 404, search and search resultspage. Now it works fine with 404 and search page without results but when I get results de menu is back... – xarai Jul 2 '13 at 14:35
  • my problem is that if(!is_404() || !is_search()) { is not working allways. It works fine on 404 and search if nothing found but it doesn't work on search page if there are results. I don't understand, it's like a bug. – xarai Jul 3 '13 at 7:59
  • I found the problem, it was part of another if/ else, excluding the 404 and search had to be earlier in the code. Thanks for al your responses! – xarai Jul 3 '13 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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