0

I am trying to add the search feature to our site. I have the search feature located in the desired position on the page so I'm happy with that. My problem is that when the results are displayed, instead of showing a standard heading like " Search Results" in the header the results page displays the title of the first page that contains the search term.

Hope that's not confusing. I need to be clear that this relates to the title in the header and not the title in the content, the title in the content is fine with "Search Results For:"

I'm thinking I need an "if,then,else" statement in the Header.php file so it uses a different title when the search feature is used.

I am working locally on my computer to try and get this done and have created a child theme of the original. In the child theme folder I have search.php, header.php, style.css and functions.php

I've been stumbling my way through this but think the following line in header.php file is where a revision is required.

<h1 class="site-title"><?php echo get_the_title($post->ID); ?></h1>  

Any help is greatly appreciated, I've spent hours on this!

David

  • Can you show to us the code of the template that you are using to display the search results? – cybmeta Mar 17 '16 at 15:12
  • Sorry that line in the header.php didn't display for some reason, I'll try again. If it shows ID); ?> again there may be a problem with getting the line displayed. Here's the line (hopefully): <h1 class="site-title"><?php echo get_the_title($post->ID); ?></h1> – David H Mar 17 '16 at 15:13
  • Let me send a link to that file search.php. That's the one, correct? – David H Mar 17 '16 at 15:15
  • Use `code here` to add inline code, or use 4 spaces at the beggining of each line to add blocks of code (you can also add blocks or code pressing {} symbol in the editor. – cybmeta Mar 17 '16 at 15:15
  • Please, don't send links, add all the relevant code to the question itself. – cybmeta Mar 17 '16 at 15:16
0

You can replace:

<h1 class="site-title"><?php echo get_the_title($post->ID); ?></h1>

With:

<h1 class="site-title"><?php _e( 'Search results for', 'theme-textdomain' ).': ' . get_search_query(); ?></h1>

But obviously only in search pages. I suggest to move the <h1> element to each template you may need instead of include it in the general header.php template. It is up to you but if you use header.php to dipslay the <h1>, you will need some kind of conditionals, not only in search results, but also in categories archives, date archives, etc.

For example:

<?php if( is_search() ) { ?>
    <h1 class="site-title"><?php _e( 'Search results for', 'theme-textdomain' ).': ' . get_search_query(); ?></h1>
<?php } else { ?>
    <h1 class="site-title"><?php echo get_the_title($post->ID); ?></h1>
<?php } ?>
|improve this answer|||||
  • I tried adding the if, else code into the header.php and get a syntax error though I did include <?php before the if – David H Mar 17 '16 at 15:38
  • I'll continue to work on it. – David H Mar 17 '16 at 15:39
  • Of course, add it if you need. I can not know where exactly you are goint to include the code. It was obvious to me that you are going to use within php tags. Answer edited. – cybmeta Mar 17 '16 at 15:51
  • Cybmeta, many thanks, the first option, replacing the "<h1 class" is sufficient for our needs I believe. I'm going to have to get up to speed rapidly with php etc! You've saved me a ton of time. Thanks again. – David H Mar 17 '16 at 16:05
  • Please, i this fixed your problem, mark the answer as accepted, so other users with the same problem can find the answer quickly. – cybmeta Mar 17 '16 at 17:26

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.