0

In the search.php I have this code →

    <div class="main col">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <?php get_template_part('content',get_post_format()); ?>
    <?php endwhile; ?>
    <?php else: ?>
        <?php get_template_part('content', 'none'); ?>
    <?php endif; ?>
</div>

Actually, It is pulling content from the content.php, which was mainly built for the index.php. There are so many things that I do not want in my search.php

Let me explain in detail, but before I do that let me show you my live page here, which is a WordPress website.

So basically in my search page result, I do not want these things →

  1. https://www.screencast.com/t/vZfIUrOc87
  2. I also do not want this much amount of text → https://www.screencast.com/t/cG0nj0lcHb
  3. I also do not want the author box → https://www.screencast.com/t/PXxueQeeVKF5

so what should I do? Do I need to create a separate file other than content.php like this☻→ content-search.php or In content.php I have to put some If___ else___ condition to customize the search result page and at the same time, the index.php shouldn't be affected.

  • I think something like this will work at the logic level in search.php → If ! serach.php then execute this code____ But i do not know How. – The WP Intermediate May 14 '17 at 18:15
  • 1
    Have you tried loading a different file instead? – Tom J Nowell May 14 '17 at 18:25
  • I have not yet created as I am novice so do not know whether it is a better practice or not should I create a file content-search.php? Is that absolutely fine? or LESS IS MORE and a better coding practice. – The WP Intermediate May 14 '17 at 18:28
  • 1
    Loading a content-search.php is a perfectly valid thing to do – Tom J Nowell May 14 '17 at 21:17
1

Checking if showing Search Results

The conditional statement you want is is_search() documented here.

Search.php and Content-search.php

Search.php is the template file that corresponds to displaying results from a search form request. If it is not present in the theme (meaning there is no search.php) then wordpress defaults to index.php and uses that.

Changes to search.php will not affect index.php.

In a similar way, content-search.php will be looked for, and if not found, then content.php will be used.

Read about Template Hierarchy here.

Customizing

That said, if you are not already, be sure you are using a Child Theme when customizing.

Codex: About Child Themes.

After you have created your child theme, you can create a copy of the parent theme's search.php and place it in the child theme directory.

From here, you can create a copy of either content.php and place it in your child theme, or copy it and rename it content-search.php.

Depending on where the items you are trying to remove are located, you may have to perform edits to both of your newly created child theme files (search.php and content.php OR content-search.php) to achieve what you want.


But in regards to your concern for best practices, the specific templates help reduce conditional statements in theme files. Child themes keep you from losing customizations when themes are updated by developers. Hope that helps.

  • Its a parent Theme. – The WP Intermediate May 15 '17 at 6:23
  • 1
    You could follow the links above and create a content-search.php if that doesn't exist. (or edit the search.php) You can always move that to a child theme later so it doesn't get overwritten by theme updates. – hwl May 15 '17 at 7:01
  • I have created the content-search.php , and written this code → <?php echo '<h1><a href="get_permalink();">' . get_the_title() . '</a></h1>'; ?> but it is not generating link whats wrong? Try clicking on any title here → click Here – The WP Intermediate May 15 '17 at 7:27
  • You there?__________ – The WP Intermediate May 15 '17 at 8:15

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.