- This topic has 10 replies, 4 voices, and was last updated 4 years, 7 months ago by Leo.
-
AuthorPosts
-
November 8, 2019 at 4:08 am #1056758Emma
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.
November 8, 2019 at 5:27 am #1056795DavidStaffCustomer SupportHi 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; }
November 8, 2019 at 7:59 am #1057073EmmaThat’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.
November 8, 2019 at 11:13 am #1057257DavidStaffCustomer SupportThe 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.
November 9, 2019 at 12:42 pm #1058093EmmaThanks. 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?
November 9, 2019 at 1:04 pm #1058107LeoStaffCustomer SupportI believe David is suggesting using the header element:
https://docs.generatepress.com/article/header-element-overview/Then his CSS would hide the normal page content so that the 404 page would just be the header element.
November 9, 2019 at 2:28 pm #1058138EmmaI 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?
November 9, 2019 at 4:47 pm #1058184LeoStaffCustomer SupportFebruary 28, 2020 at 5:25 pm #1180166DanaHi 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.
February 28, 2020 at 5:40 pm #1180173DanaLooks 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');
February 28, 2020 at 6:01 pm #1180179LeoStaffCustomer SupportGlad you’ve figured out 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.