首页 > 解决方案 > 获取 wordpress 中搜索结果的总数

问题描述

如何在wordpress的搜索结果页面中获取结果总数......我想我的问题很清楚。我需要搜索结果页面中显示的搜索结果总数。还需要找到来自页面的结果并分别发布
我尝试过的是

    <?php echo count($posts); ?>

通过使用这个我得到了搜索结果的总数。但我还需要搜索结果中的页数和帖子数

标签: wordpresssearchcount

解决方案


一旦您已经在搜索查询中,您也可以使用全局 $wp_query。

请参见下面的示例:

<?php
   global $wp_query;
   $not_singular = $wp_query->found_posts > 1 ? 'results' : 'result'; // if found posts is greater than one echo results(plural) else echo result (singular)
   echo $wp_query->found_posts . " $not_singular found";
?>

推荐阅读