Site logo

Archived topic

Page Header for search results

7 replies · Started by Altti on October 15, 2017

Viewing posts 1–8 of 8

Hey, I'm trying to do a Page Header for search results. How can I include the search term in there? I've tried to do it like this but the PHP doesn't work. Any ideas?

<h1>
Search results for: <?php echo get_search_query(); ?>
</h1>

Hi there,

You can create a shortcode, which you can then add to the page header.

add_shortcode( 'search', 'tu_search_shortcode' );
function tu_search_shortcode() {
    ob_start(); ?>
        Search results for: <?php echo get_search_query(); ?>
    <?php
    return ob_get_clean();
}

With that added, you can use the [search] shortcode.

Awesome. Thanks Tom, again.

You're welcome :)

Perfect, but how can I hide the page title on the search results page for avoiding the duplicate sentence?

Good point!

You could hide it:

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

Or if you want to remove the markup completely, you would need to make a copy of the search.php file and add it to your child theme.

Thanks Tom,
I removed entirely the header thing in the search.php file, is it correct?

<header class="page-header">
     <h1 class="page-title">
	<?php
	printf( // WPCS: XSS ok.
	/* translators: 1: Search query name */
	__( 'Search Results for: %s', 'generatepress' ),
	'<span>' . get_search_query() . '</span>'
	);
	?>
     /h1>
</header><!-- .page-header -->

Correct :)

This archived topic is closed to new replies.