I've to add just a line when the user clicks on a category link or search for something, so I thought to do something like this inside the index.php instead of creating other template file like search.php and category.php:

<?php if(is_search()): ?>
    <h1><?php the_search_query(); ?></h1>
<?php endif; ?>

<?php if(is_category()): ?>
    <h1><?php single_cat_title(); ?></h1>
<?php endif; ?>

Is it a good practice? If not, why? And which is better speaking about speed?


Before I start, it will be a good idea to check out my answer to the following post

It will give you some more insight

As I stated in the linked answer, You can have a fully functional website with just index.php as template to display posts with for all pages

What you are doing is fine, and if there are any speed difference, it should be very very minute. I would however stress that your approach might have the drawback of one templates having to do to much.

Because you would want to do things differently for different pages, it will be a good idea to create those templates needed the relevant page loads. As I said, if there is any speed difference with any method, it would be really very very minute which you will not worry about. Having different templates for different pages with different layouts do have the advantage of that you do not overload one template with a lot of unnecessary code.

For extra reference, see my answer to the following post as well


For me, it's perfect to use that sort of IF logic statements in Index than writing new template files. In fact, what you're doing in your example code is already there in themes like TweentyTwelve etc. However, if you see any chance of need for different HTML code structure in future, it's better to keep the template files separate apart. So, give it a thought; see if you just need to have a different Title for page then imho, IF statements are good to go.

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.