admin管理员组

文章数量:1130349

I'am wondering how to target a search result page within the functions.php file in wordpress.

I know about "is_search" for the search page but what about the search result page ?

Can we put a condition with the query or something?

Thanks in advance.

All the best

I'am wondering how to target a search result page within the functions.php file in wordpress.

I know about "is_search" for the search page but what about the search result page ?

Can we put a condition with the query or something?

Thanks in advance.

All the best

Share Improve this question asked Nov 22, 2018 at 22:53 FrancoisFrancois 251 silver badge6 bronze badges 9
  • Hi, what exactly are you trying to achieve? It will be easier to solve this using some examples, I guess... – Krzysiek Dróżdż Commented Nov 22, 2018 at 23:29
  • There is no standard "search page". is_search() is for the search results page. – Jacob Peattie Commented Nov 23, 2018 at 0:03
  • I'm not sure what you mean by search result page, the page that shows the results of a search query will return true for is_search, there are no other pages – Tom J Nowell Commented Nov 23, 2018 at 0:09
  • Hello guys, Thanks for answering. I mean by result page, the page whose URL contains ?s= The page displaying the results after the user query on the search.php page. Thanks – Francois Commented Nov 23, 2018 at 16:59
  • @TomJNowell yes indeed is_search is the search result page.. thank you – Francois Commented Nov 26, 2018 at 23:17
 |  Show 4 more comments

1 Answer 1

Reset to default 0

I'am wondering how to target a search result page within the functions.php file in wordpress.

Use is_search, and the search.php template

I know about "is_search" for the search page but what about the search result page ?

They are the same thing, is_search indicates the main query is a search query. When is_search is true, WordPress loads search.php as the template, falling back to archive.php then index.php if they aren't present.

I'm not sure where the idea of a separate search page, and a separate search results page came from, but that's not how it works. There are search results, and a search form that can appear in any place, but no search page.

Can we put a condition with the query or something?

If you want to modify the main query, use the pre_get_posts filter. This is true for all queries, not just search queries, and allows you to intercept the query parameters before WordPress fetches the posts to change what's fetched.

I'am wondering how to target a search result page within the functions.php file in wordpress.

I know about "is_search" for the search page but what about the search result page ?

Can we put a condition with the query or something?

Thanks in advance.

All the best

I'am wondering how to target a search result page within the functions.php file in wordpress.

I know about "is_search" for the search page but what about the search result page ?

Can we put a condition with the query or something?

Thanks in advance.

All the best

Share Improve this question asked Nov 22, 2018 at 22:53 FrancoisFrancois 251 silver badge6 bronze badges 9
  • Hi, what exactly are you trying to achieve? It will be easier to solve this using some examples, I guess... – Krzysiek Dróżdż Commented Nov 22, 2018 at 23:29
  • There is no standard "search page". is_search() is for the search results page. – Jacob Peattie Commented Nov 23, 2018 at 0:03
  • I'm not sure what you mean by search result page, the page that shows the results of a search query will return true for is_search, there are no other pages – Tom J Nowell Commented Nov 23, 2018 at 0:09
  • Hello guys, Thanks for answering. I mean by result page, the page whose URL contains ?s= The page displaying the results after the user query on the search.php page. Thanks – Francois Commented Nov 23, 2018 at 16:59
  • @TomJNowell yes indeed is_search is the search result page.. thank you – Francois Commented Nov 26, 2018 at 23:17
 |  Show 4 more comments

1 Answer 1

Reset to default 0

I'am wondering how to target a search result page within the functions.php file in wordpress.

Use is_search, and the search.php template

I know about "is_search" for the search page but what about the search result page ?

They are the same thing, is_search indicates the main query is a search query. When is_search is true, WordPress loads search.php as the template, falling back to archive.php then index.php if they aren't present.

I'm not sure where the idea of a separate search page, and a separate search results page came from, but that's not how it works. There are search results, and a search form that can appear in any place, but no search page.

Can we put a condition with the query or something?

If you want to modify the main query, use the pre_get_posts filter. This is true for all queries, not just search queries, and allows you to intercept the query parameters before WordPress fetches the posts to change what's fetched.

本文标签: conditional tagsHow to target a search result page