0

I have a problem with the style of my search page, and I'd like to fix it. However, I'm not sure which template I need to modify. How do I find out which template is being called by my theme when I use WordPress's default search widget?

Thanks!

  • 1
    Maybe try this answer. Upvote if helpful, this question may be a possible duplicate of Get name of current Template File. – Howdy_McGee Mar 29 '17 at 17:27
  • That snippet in the answer you linked to did the job, it helped me identify the template (which was search.php). I don't think this question is a duplicate of that question, because this is more specific. I don't know what the community rules are about this. How I should handle this? – CoderScissorhands Mar 29 '17 at 17:34
  • It's just a possible match, it doesn't mean anything of the questions are different, no worries. – Howdy_McGee Mar 29 '17 at 17:39
0

The file that is displaying the search result, is the search.php file from the main theme folder.

Below, you can find a working example that does the search of the default posts and pages:

'post_type' => array('post', 'page')

You can add a custom post in that array, for instance, if the custom post type has the 'portfolio' name then add it to the array will be:

'post_type' => array('post', 'page', 'portfolio')

The whole search.php example:

<?php
/* The template for displaying Search Results pages.*/

get_header();
?>

                <!-- main -->
                <div id="main">

                <!-- intro -->
                <section class="intro">
                    <!-- search -->
                    <form action="<?php echo home_url(); ?>" method="get" class="search">
                        <fieldset>
                            <input type="text" name="s" id="s" value="<?php _e('Click or type here to search','text_domain'); ?>" class="text" >
                            <input type="submit" value="go" class="submit" >
                        </fieldset>
                    </form>
                    <p><?php printf( __( 'Search Results for: %s', 'text_domain' ), '<strong>' . get_search_query() . '</strong>' ); ?></p>
                </section>
                <div class="main-holder">
                    <!-- search results list -->
                    <section class="col" id="content">
                        <?php
                            $s = $_GET['s'];

                            $args=array(
                                'post_type' => array('post', 'page'),
                                'post_status' => 'publish',
                                's' => $s,
                                'orderby' => 'ID',
                                'order' => 'desc',
                                'paged' => $paged
                            );
                            $temp = $wp_query;  // assign original query to temp variable for later use   
                            $wp_query = null;

                            $wp_query = new WP_Query($args);
                            if ($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post();
                                $thumb_url = get_the_post_thumbnail($post->ID, '', array('alt' => the_title_attribute('echo=0')));
                        ?>

                        <!-- article -->
                        <article class="article article-alt">
                            <div class="heading">
                                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                            </div>
                            <nav class="add-info">
                                <ul>
                                    <li><?php echo get_the_time('F d, Y'); ?></li>
                                    <li class="data"><?php the_category(', ') ?></li>
                                    <li class="data"><?php echo $cat_slug; ?></li>
                                    <li><?php comments_popup_link(__('0 Comments', 'text_domain'),__('1 Comment', 'text_domain'), __('% Comments', 'text_domain')); ?></li>
                                </ul>
                            </nav>
                            <?php if ($thumb_url) { ?><figure class="visual"><?php echo $thumb_url; ?></figure><?php } ?>
                            <?php the_excerpt(); ?>
                            <a class="more" href="<?php the_permalink(); ?>"><?php _e('Read more ...','text_domain'); ?></a>
                        </article>

                        <?php 
                            endwhile; 
                                else: 
                                    _e('<h2>Nothing Found</h2>','text_domain');
                                    echo '<p>'.__( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'text_domain' ).'</p>';
                            endif;
                        ?>

                        <!-- paging -->
                        <nav class="paging">
                            <ul>
                                <?php
                                    if(function_exists('wp_pagenavi')) { wp_pagenavi(); }                               
                                ?>
                            </ul>
                        </nav>

                        <?php
                            $wp_query = null;
                            $wp_query = $temp;
                        ?>

                    </section>
                    <!-- sidebar -->
                    <aside class="col" id="sidebar">
                        <?php dynamic_sidebar("Blog Sidebar") ?>
                    </aside>
                </div>
            </div>
            <!--/ main -->
  • wow, thank you for taking the time, but the question is how do identify the file evoked by the search widget, often it is search.php, but it isn't always. I was looking for a general answer about how to ID the file for any theme. I don't think you answered it, am I mistaken? – CoderScissorhands Mar 29 '17 at 18:20
0

I don't have an answer specifically about how to ID the template of the search page, but this solution will allow you to ID the template of mostly any page you are viewing, including the search page (which is what I wanted). I got this answer using the advice provided by @Howdy_Mcgee in the comment above.

  1. Enter this snippet in your function.php file (from here, thanks to @Howdy_Mcgee) and save the changes. It should cause WP to print the name of the current PHP template for any page (including the search results page) that is displayed:

    add_action('wp_head', 'show_template');
      function show_template() {
      global $template;
      echo basename($template);
     }
    
  2. Type a search term in your site's search widget's form and hit enter, making sure to use some terms that are actually in your site's content. This should trigger your site to call the template it uses to return results.

  3. Look at the search results page, the name of the template used by the search widget should be displayed somewhere on the page, and probably at the top.

When I these three things, in my case, the page being used by the default WordPress widget showed me my search results were being displayed with search.php, but it may be different for your theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.