Site logo

Archived topic

Page header on search page

10 replies · Started by Emma on November 8, 2019

Viewing posts 1–11 of 11

Hi, I'm building a site with GeneratePress using the header element to create different headers for archives, categories, posts and pages. I am having a bit of a problem creating a header for the 'search results' page. It doesn't seem to display the text within the header, it displays it below.

With all the other pages, posts, category pages and archive pages it displays the relevant text within the header, however on the search result page, it displays the header image with no text inside. I must have done something wrong but I'm not sure exactly what... Any advice regarding this would be much appreciated.

Thank you.

Hi there,

if you're using the {{post_title}} template tag it won't work on the Search Results as it is dynamically generated so not content title exists for it to display.

You would need to create a shortcode - add this PHP snippet:

function db_display_search_query() {
    ob_start();
    printf( __( 'Search Results for: %s', 'generatepress' ), get_search_query() );
    return ob_get_clean();
}
add_shortcode( 'search-title', 'db_display_search_query' );

Then create a new header element just for search results and use the shortcode in your HTML:

<h1>[search-title]</h1>

Then use this CSS to remove the existing title:

.search-results .page-header {
    display: none;
}

That's great! Thank you. Am I able to do something similar with the 404 page? I need to include a header element as the nav is merged so I need to put some content in it.

The 404 would require its own header element that you can add whatever content you wish. And you can use this CSS to hide the main content:

.error404 #page {
    display: none;
}

So the Header element effectively becomes the 404.

Thanks. I am not sure how to add the content for the 404 page into the header. I can add the text but I am not sure how to add the search bar into the header. Is there a simple way to do this?

I am using the header element. I have a header set up for the 404 page but I am not sure how to put the search bar inside the header. Can you advise how I do this?

Hi There,

I used the shortcode from this thread for my custom search page.

I would like to return the count of the number of posts with the keyword in my search results using a shortcode. I tried on my own but have been failing.

Do you have a shortcode or function already in your bag of tricks I could use?

eg. "There are x matches for [search-title]"

Thanks.

Looks like I answered my own question again.

Here is the solution for anyone else who may be looking to do the same:

function db_display_search_query_count() { 
	global $wp_query;
	$total_results = $wp_query->found_posts;
	return $total_results;
} 
add_shortcode('result-matches','db_display_search_query_count');

Glad you've figured out :)

This archived topic is closed to new replies.