I have a specific CSS-file for my search results, so the search.php template. I want a PHP if-condition to determine if the current template is the search.php template, so that I can load that CSS-file with wp_enqueue. The condition should work in the functions.php file.
I’m not entirely sure if I can use is_search() for this. Can anyone confirm I can use is_search() and, if that’s not the case, provide an alternative?
Many thanks in advance!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
WordPress conditional functions will not tell you which template file is used. They tell you what the current request is for. Which file is loaded for that type of request is determined by the template hierarchy.
So, is_search() will tell you if search.php is being used if the current request is a search, and the current theme includes a search.php. If the theme does not include search.php, is_search() will still return true if index.php is used, as per the template hierarchy.
That’s the long way of saying that if your theme has search.php, then yes, use is_search().
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0