Support » Fixing WordPress » search.php

  • kgmyat

    (@kgmyat)


    Hello everyone, I’m currently using twenty sixteen theme.

    If you type something in search box (eg.show), and if the keyword matches with a post in your website, it show that post off the bat.
    But, if your keyword (eg. sho w) has a little conflict with the post, or that keyword include in other posts, the page “Search Results for:” shows all the relevant posts of your website.
    If the keyword matches nothing, it shows “Sorry, but nothing matched your search terms. Please try again with some different keywords.”
    or
    “Oops! That page can’t be found”

    Ok,
    I would like to get my website’s users to the posts those I created. If the keywords don’t match anything, I like “Oops! That page can’t be found” . And I can make it.

    What I really want is not to display “Search Results for:” page (with relevant posts) and replace with my custom page or post, if not possible, with my custom text.

    In search.php file, the author said:
    Run the loop for the search to output the results.
    If you want to overload this in a child theme then include a file
    called content-search.php and that will be used instead.

    But, I don’t know how to code content-search.php file.
    All I want is simple, let the theme be, but want to replace, (if not possible) remove
    “Search Results for:” page (with relevant posts)

    Please someone guide me, help me with this.

Viewing 13 replies - 1 through 13 (of 13 total)
  • Joey

    (@leglesslizard)

    To override theme templates you will need to create a child theme. Have a read here on how this can be achieved.

    Once you have a child theme any template file named the same as the parent theme file (e.g. search.php or content-search.php) will be used instead of the one from the parent theme.

    The easiest way to modify these templates is to copy and paste the original template from your parent theme to the child theme and just tweak it to suit your needs.

    I hope that helps 🙂

    kgmyat

    (@kgmyat)

    Hi, joey, I know how to make child theme, and I’ve already one for twenty sixteen. I don’t know how to tweak in content-search.php
    All I need is coding for my solution.

    Joey

    (@leglesslizard)

    content-search.php is the file for editing what is displayed if search content is found. For example displaying more or less post data (title, image, excerpt etc.) for each result returned.

    From the sounds of it you want to change the page title is that right? If you want to edit the text “Search Results for:” then you want to look at the search.php template. Just copy the whole file into your child theme, this will automatically override the parent template. Then make your changes 🙂 for the page title you’d be looking at this section:

    <header class="page-header">
      <h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentysixteen' ), '<span>' . esc_html( get_search_query() ) . '</span>' ); ?></h1>
    </header><!-- .page-header -->

    Apologies but from your initial question I don’t understand specifically what changes you want to make. But whatever they are the process will be the same.

    Regards

    kgmyat

    (@kgmyat)

    Thanks you for your concern, but Joey, what I mean is not ‘Search Results for: %s’.
    This is just a text.
    Under that ‘Search Results for: %s’,
    relevant post titles (those you made in your website) are displayed.

    What I mean is absolutely remove

      ‘Search Results for: %s’ function

    (that function shows my post titles which are relevant with user’s keyword)

    Not just remove the text ‘Search Results for:’
    Can you make it?

    Joey

    (@leglesslizard)

    Sorry mate but I’m still unclear. If I break down the template for you maybe that will help some? You can add/remove whatever you want from the template as long as it still adheres to coding standards. So, for example, you could remove the “loop” which shows the posts for the search page and the template would still work fine but it wouldn’t make a lot of sense to do this with a search page…

    <?php
    /**
     * The template for displaying search results pages
     *
     * @package WordPress
     * @subpackage Twenty_Sixteen
     * @since Twenty Sixteen 1.0
     */
    
    get_header(); ?>
    
    	<section id="primary" class="content-area">
    		<main id="main" class="site-main" role="main">
    
    		<?php if ( have_posts() ) : ?>
    
    			<header class="page-header">
    				<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentysixteen' ), '<span>' . esc_html( get_search_query() ) . '</span>' ); ?></h1>
    			</header><!-- .page-header -->
    
    			<?php
    			// Start the loop.
    			while ( have_posts() ) : the_post();
    
    				/**
    				 * Run the loop for the search to output the results.
    				 * If you want to overload this in a child theme then include a file
    				 * called content-search.php and that will be used instead.
    				 */
    				get_template_part( 'template-parts/content', 'search' );
    
    			// End the loop.
    			endwhile;
    
    			// Previous/next page navigation.
    			the_posts_pagination( array(
    				'prev_text'          => __( 'Previous page', 'twentysixteen' ),
    				'next_text'          => __( 'Next page', 'twentysixteen' ),
    				'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentysixteen' ) . ' </span>',
    			) );
    
    		// If no content, include the "No posts found" template.
    		else :
    			get_template_part( 'template-parts/content', 'none' );
    
    		endif;
    		?>
    
    		</main><!-- .site-main -->
    	</section><!-- .content-area -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    So get_header() just grabs the header template.
    The next 2 lines open the starting html tags (section and div).
    The next statement is what you are interested in. This says “if we have posts to show insert header and loop through the posts”. This header is the text we referred to earlier (Search Results for: %s).
    The next statement (while ( have_posts() ) : the_post();) loops through all of the search results and pulls in the content-search.php template for each post. This loop and the pulled in template are what generates the results (title, image, text) for each post displayed on the search results page.
    Then your pagination links are inserted and the loop is closed along with the html tags.

    Does that give you a better idea on how to edit the file? If, for example, you wanted to insert a completely custom page instead of displaying search results you could remove the “while” loop (everything from while – end while;) and write whatever you wanted to display there. Then the page would show the header text “Search results for..” and whatever you wanted underneath.

    Regards

    kgmyat

    (@kgmyat)

    Thanks for your reply. But Joey, I’m not a professional coder. It is very close what I want.
    Yes, I want to remove the loop, and it can be done by removing “while” loop (everything from while – end while;).

    But, how to replace the gap underneath [contents under the title“Search results for..”](I mean relevant posts and pages) with my custom text or my custom post or page?
    Please, could you make some example code?
    It really helpful to me. Thanks you so much.

    Joey

    (@leglesslizard)

    OK no problem but I like to try and make sure you understand what you are doing with the hope you can go back and make edits in the future 🙂 so the “<?php” and “?>” are opening and closing tags for PHP code respectively. Outside of these tags the template will contain HTML which is the code interpreted by the web browser and dictates what is displayed on the screen. The PHP is interpreted server side before the HTML. Using PHP we can create loops etc to create dynamic content and output HTML using that content whereas HTML is used for displaying static content only.

    I have edited the template below with some custom HTML code instead of the post loop as you have stated.

    <?php
    /**
     * The template for displaying search results pages
     *
     * @package WordPress
     * @subpackage Twenty_Sixteen
     * @since Twenty Sixteen 1.0
     */
    
    get_header(); ?>
    
    	<section id="primary" class="content-area">
    		<main id="main" class="site-main" role="main">
    
    		<?php if ( have_posts() ) : ?>
    
    			<header class="page-header">
    				<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentysixteen' ), '<span>' . esc_html( get_search_query() ) . '</span>' ); ?></h1>
    			</header><!-- .page-header -->
    
                            <div class="custom-content">
                                <h2>This is a custom header</h2>
                                <p>This is a custom paragraph</p>
                            </div>
    
    		<?php
    		// If no content, include the "No posts found" template.
    		else :
    			get_template_part( 'template-parts/content', 'none' );
    
    		endif;
    		?>
    
    		</main><!-- .site-main -->
    	</section><!-- .content-area -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
    Joey

    (@leglesslizard)

    If you have a certain post/page to display you can pull in the content as shown below. But you will need to know the ID of the specific post/page or have a way to find it. This is untested but should give you the title and content from whatever post/page ID you choose, I have used 1 to demonstrate:

    <?php
    /**
     * The template for displaying search results pages
     *
     * @package WordPress
     * @subpackage Twenty_Sixteen
     * @since Twenty Sixteen 1.0
     */
    
    get_header(); ?>
    
    	<section id="primary" class="content-area">
    		<main id="main" class="site-main" role="main">
    
    		<?php if ( have_posts() ) : ?>
    
    			<header class="page-header">
    				<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentysixteen' ), '<span>' . esc_html( get_search_query() ) . '</span>' ); ?></h1>
    			</header><!-- .page-header -->
    
                    <?php $custom_post = get_post( 1 ); ?>
                    <?php $content =  apply_filters( 'the_content', $custom_post->post_content ); ?>
                            <div class="custom-content">
                                <h2><?php echo $custom_post->post_title; ?></h2>
                                <?php echo $content; ?>
                            </div>
    
    		<?php
    		// If no content, include the "No posts found" template.
    		else :
    			get_template_part( 'template-parts/content', 'none' );
    
    		endif;
    		?>
    
    		</main><!-- .site-main -->
    	</section><!-- .content-area -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
    kgmyat

    (@kgmyat)

    Thank you for your kindly reply. Joey.
    Wordpress community is great, because of you guys.
    And Happy New Year, be good luck for the whole year.

    Joey

    (@leglesslizard)

    Open source tends to mean everyone helps each other out 🙂 it is a great community. Same to you!

    All the best 🙂

    kgmyat

    (@kgmyat)

    This code really helpful to me. It link to my specific post/page.
    But, one more thing in this code, Joey.
    In my child-theme, I put some code not to show post or page title.

    With your code, the title of (post/page) display, (under Search Results for:)
    Please guide me how to hide that title of specific page/post.

    Joey

    (@leglesslizard)

    If you don’t want the page header delete the following:

    <header class="page-header">
    				<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentysixteen' ), '<span>' . esc_html( get_search_query() ) . '</span>' ); ?></h1>
    			</header><!-- .page-header -->

    If you want to remove the specific post/page title delete this part:
    <h2><?php echo $custom_post->post_title; ?></h2>

    Regards

    kgmyat

    (@kgmyat)

    Thank again. Joey.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘search.php’ is closed to new replies.