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 want one image in header.php to change for different pages.

The function in header.php looks like that:

if(is_archive()):
  //change image to "Archives"
endif;
if(is_404()):
 //change image to "404"
endif;

Now, is it possible to check if page is the search page AND what's the search string AND how many posts were found?

So what I want to do is basically:

if(is_search()):
 //change image to "Search" AND echo %s
endif;
if(is_search_empty()): //yes I made this up
 //change image to "Nothing found" AND echo $
endif;

Is it even possible outside the search.php page loop?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Now, is it possible to check if page is the search page

is_search()

AND what's the search string

is_search() AND print get_query_var( 's' )

AND how many posts were found?

is_search() AND print $GLOBALS['wp_query']->number_posts()

… or if you got a custom query…

$my_query = new WP_Query( array( /* whatever args */ ) );
is_search() AND print $my_query->number_posts()
share|improve this answer
    
Thank you, but checking how many posts were found still doesn't seem to work for me. I have standard if( have_posts() ) : loop in search.php and your first code returns Fatal error: Call to undefined method WP_Query::found_posts(). –  Wordpressor Mar 4 '13 at 3:06
    
Try $GLOBALS['wp_query']->number_posts(). Have written it from the backside of my head and not sure what the method name is. –  kaiser Mar 4 '13 at 3:59
    
$GLOBALS['wp_query']->have_posts() was the answer, thanks a lot, Franz! –  Wordpressor Mar 4 '13 at 4:14
add comment

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.