[Resolved] Page Header for search results

Home Forums Support [Resolved] Page Header for search results

Home Forums Support Page Header for search results

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #403477
    Altti

    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>
    #403642
    Tom
    Lead Developer
    Lead Developer

    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.

    #404046
    Altti

    Awesome. Thanks Tom, again.

    #404256
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

    #449036
    Gabriele

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

    #449126
    Tom
    Lead Developer
    Lead Developer

    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.

    #449254
    Gabriele

    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 -->
    #449491
    Tom
    Lead Developer
    Lead Developer

    Correct ๐Ÿ™‚

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.