admin管理员组文章数量:1287596
Until now I have been using below code to get the number of results when someone searches and to display that count.
<?php /* Search Count */ $allsearch =& new WP_Query("s=$s&showposts=-1"); $count = $allsearch->post_count; echo $count . ' '; wp_reset_query(); ?>
But this does not seem like valid code. It shows below error:
Deprecated: Assigning the return value of new by reference is deprecated
Can anyone please suggest the proper way in which I get get the search count. The above code is placed in the heading of my index.php file of theme within a conditional statement to display different heading based on what type of page a user is on.
Until now I have been using below code to get the number of results when someone searches and to display that count.
<?php /* Search Count */ $allsearch =& new WP_Query("s=$s&showposts=-1"); $count = $allsearch->post_count; echo $count . ' '; wp_reset_query(); ?>
But this does not seem like valid code. It shows below error:
Deprecated: Assigning the return value of new by reference is deprecated
Can anyone please suggest the proper way in which I get get the search count. The above code is placed in the heading of my index.php file of theme within a conditional statement to display different heading based on what type of page a user is on.
Share Improve this question asked Aug 3, 2013 at 15:52 Harish ChouhanHarish Chouhan 7953 gold badges10 silver badges22 bronze badges 1 |2 Answers
Reset to default 43If you are within the search template i.e Search query is your main query. You should then be able to get search results from global $wp_query
without running an additional query.
global $wp_query;
echo $wp_query->found_posts.' results found.';
Edit 1
If you have to get count out of search context. You can combine both techniques to get efficient result. It wont fetch all the post but you can get the search count.
$allsearch = new WP_Query("s=$s&showposts=0");
echo $allsearch ->found_posts.' results found.';
Your Error
About the error you are getting, it lies here
$allsearch =& new WP_Query("s=$s&showposts=-1");
Remove the "&" beside the equal sign to get rid of the error. So it will look like this
$allsearch = new WP_Query("s=$s&showposts=-1");
Might be useful also to share this, so that your first heading notifies the user search results were found and the second, notifies the user how many were found. If it was less than 2 results, it should read '1 result found' else 'x results found'.
<h2 class="blog_archive_heading">Search Results for '<?php the_search_query(); ?>'</h2>
<h3 class="blog_archive_heading"><?php
global $wp_query;
if($wp_query->found_posts < 2) {
$result = "result";
} else {
$result = "results";
}
echo $wp_query->found_posts . " " . $result . " found.";
?></h3>
This code snippet appears in my search.php so to speak.
section-searchresults.php will simply output the blog posts that were found related to your search, if none were found then it will output 'No results found'.
本文标签: theme developmentDisplay Search Result Count
版权声明:本文标题:theme development - Display Search Result Count 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741309976a2371594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
allsearch = new WP_Query
without '&' to avoid deprecated notice. – gmazzap Commented Aug 3, 2013 at 16:04