Question

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!

Était-ce utile?

La solution

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().

Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top