• Resolved jesperc

    (@jesperc)


    Hi everyone!

    I am trying to customize my search results page in the twenty twenty theme and I have developed this search.php

    <?php 
    /**
     * Template Name: Search Page <div class="search-post-link" style="margin-bottom: 10px;"><a href="<?php the_permalink(); ?>"><?php the_permalink(); ?></a></div>
     */
    ?>
    
    <?php get_header(); ?>
    <main id="site-content" role="main">
    
    	
    <?php /* Start the Loop */ ?>
    		<style>
    			p
    			{
    				margin-left:auto;
    				margin-right:auto;
    				max-width:58rem;
    				width: calc(100% - 4rem);
    				text-align:left;
    				
    			}
    		</style>
    	
    			
            <?php while ( have_posts() ) : the_post(); ?>
            <div class="container" style="font-size: 21px;">
                <div class="row">
                    <div class="col-md-12">
    
    		<header class="entry-header has-text-align-center">
    			<h3><a href="<?php the_permalink(); ?>" style="color:#fff"><?php the_title(); ?></a></h3>
    		
    		</header>
            <div class="search-post-excerpt has-text-align-center" style="color:#fff">
    			<p>
    				<?php the_excerpt(); ?>
    			</p>
    		</div>
            
            <hr>
                     </div>
                </div>
            </div>
            <?php endwhile; ?>
    
    		the_posts_pagination(
    				array(
    					'prev_text'          => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
    					'next_text'          => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
    					'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
    				)
    			);
    
    <?php get_footer();

    Now I want there to be a title confirming what the user searched for in the header however I have only successfully placed it above or below the header. The code for the title would be the following:

    "Search for: "<?php the_search_query(); ?>"";

    but I don’t know how to put it in the header with white color.

    Also I am localhosting my site so I can’t share it but I hope I have provided enough information for you to see what I mean.

    Thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter jesperc

    (@jesperc)

    Also the text below is what I imported from the twenty seventeen theme to be able to go to the next search results page. I would love help fixing that function for the 2020 theme.

    Moderator bcworkz

    (@bcworkz)

    If you want the search title IN the header, you’d need to alter the header.php template. Place your search title code within a if ( is_search() ) : conditional so it doesn’t show up on other pages.

    What is it about twentytwenty’s pagination template part that you don’t like? Why try to use twentyseventeen code?

    Thread Starter jesperc

    (@jesperc)

    I’m still very new to programming so I have no clue where to place your code. The problem with the pagination is it wouldn’t show. I didn’t know what to search for to find one that works with 2020.

    Moderator bcworkz

    (@bcworkz)

    In case you are not aware, updates to twentytwenty will wipe out any changes you make to the theme. To ensure your custom work remains safe, you should create a child theme.

    Keep your search.php template and any other templates you might alter, like header.php in the child theme’s folder.

    Where to place the code depends on where you want the text to appear. It does go on a copy of header.php kept in the child theme. It must be after the <body> tag. Pay attention to whether your chosen location is in a <?php ?> block or not. You cannot nest <?php ?>, but you might need to add a new one if your chosen location is among HTML content. It might take a bit of trial and error to zero in on the right location.

    To start with, find a spot among HTML content and add this:

    <?php if ( is_search() ) {
      echo "Search for: "; the_search_query();
    } ?>
    Thread Starter jesperc

    (@jesperc)

    Thanks a lot, I finally made it work! I had to create a custom div class in the echo to be able to customise the title a bit in the CSS too

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding a title to my search.php’ is closed to new replies.