如何将真实的搜索结果计数'在搜索结果出现在页面上之前?
<div><?php echo $search_result_count.' RESULTS'; ?></div>
<div class="search-results">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php get_template_part('/include/pattern','listings'); ?>
<?php $search_result_count++ ?>
<?php endwhile; // end of the loop. ?>
</div>
<div><?php echo __('THERE ARE '.$search_result_count.' RESULTS'); ?></div>
目前显示为...
0 RESULTS
- LISTING #1
- LISTING #2
- LISTING #3
- LISTING #4
THERE ARE 4 RESULTS
我想要的是......
4 RESULTS
- LISTING #1
- LISTING #2
- LISTING #3
- LISTING #4
THERE ARE 4 RESULTS
必须有几种方法可以做到这一点..任何想法?
答案 0 :(得分:0)
如果你正在使用PHP和MySQL,那么使用mysqli_num_rows就可以得到一个非常简单的解决方案。
示例代码 -
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("Result set has %d rows.\n",$rowcount);
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
答案 1 :(得分:-1)
首先浏览顶部的循环(原始代码之前):
<?php
$search_result_count = 0;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$search_result_count++;
endwhile;
rewind_posts(); // Resets the loop
endif;
?>
.... your original code
现在,您可以在显示帖子之前使用$search_result_count
。
除非有大量帖子,否则不应该出现任何性能问题(比您使用Wordpress时已经存在的更多。是的......我去了那里! )。