Conditional to determine if search.php page is the current template

I have a specific CSS-file for my search results, so the search.php template. I want a PHP if-condition to determine if

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!

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!

Share Improve this question edited Jul 6, 2020 at 16:24 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jul 6, 2020 at 13:07 ralphjsmitralphjsmit 4026 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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().

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742288089a4415685.html

相关推荐

  • Conditional to determine if search.php page is the current template

    I have a specific CSS-file for my search results, so the search.php template. I want a PHP if-condition to determine if

    19小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信