Query total number of posts

时间:2017-05-12 作者:JoaMika

使用搜索过滤器插件。我使用获取数据库中存在的帖子总数

 $wp_query->found_posts
但是,当用户过滤页面上的结果时,该数字会根据过滤器上显示的帖子数量而变化。

我怎样才能得到不管用户过滤什么都不会改变的静态帖子总数?

更新:这是我的完整模板代码。我尝试了下面的答案,但无法使用我的模板。有什么想法吗?

if ( $query->have_posts() )
{
    ?>
<ul id="florefs">
    <?php
    while ($query->have_posts())
    {
        $query->the_post();

        ?>

        <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><i class="x-icon florex x-icon-angle-right" data-x-icon="" aria-hidden="true"></i>&nbsp;<?php the_field(\'br_name\'); ?></a>
        <div id="flosex"><span class="fimi"><?php the_field(\'br_category\'); ?></span><span class="flag <?php echo strtolower(get_field(\'br_heritage\')); ?>"></span><span class="fama"><?php the_field(\'br_heritage\'); ?></span></div></li>

        <?php
    }
    ?>
</ul>
    <div class="filhead">Page <?php echo $query->query[\'paged\']; ?> of <?php echo $query->max_num_pages; ?></div>

    <div class="pagination">

        <div class="nav-next"><?php previous_posts_link( \'<i class="x-icon x-icon-arrow-left" data-x-icon="" aria-hidden="true"></i> Previous page\' ); ?></div>
        <div class="nav-previous"><?php next_posts_link( \'Next page <i class="x-icon x-icon-arrow-right" data-x-icon="" aria-hidden="true"></i>\', $query->max_num_pages ); ?></div>

        <?php
            /* example code for using the wp_pagenavi plugin */
            if (function_exists(\'wp_pagenavi\'))
            {
                echo "<br />";
                wp_pagenavi( array( \'query\' => $query ) );
            }
        ?>
    </div>

<div id="prasti"><span class="prase">PROBLEM HERE</span><span class="praso"><?php echo $query->found_posts; ?></span><span class="prasif">NEEDS</span><span class="prasi">CHANGE</span></div>

    <?php
}
else
{
    echo "There are no results for your selected criteria.";
}
?>

2 个回复
最合适的回答,由SO网友:hwl 整理而成

我怎样才能得到不管用户过滤什么都不会改变的静态帖子总数?

您可能正在寻找wp_count_posts(): Codex: WP COUNT POSTS

获取所有已发布帖子数的示例:

function get_all_them_posts(){
    $count_posts = wp_count_posts();

    $published_posts = $count_posts->publish;
    return $published_posts;
}
在模板中:

 <?php echo get_all_them_posts(); ?>

For Custom Post Type:

功能。php:

function get_all_them_cpt_posts(){
    $post_type = \'your_post_type_slug_here\';
    $count_posts = wp_count_posts( $post_type );

    $published_posts = $count_posts->publish;
    return $published_posts;
}
在模板中:

 <?php echo get_all_them_cpt_posts(); ?>
如所述Sam, 并且在this older WPSE answer, $found_posts 是指查询所包含的内容。$post_count 是指正在显示的内容(通常是在posts\\u per\\u page参数中设置的数字)。我想wp_count_posts() 这就是你想要的。

对于更新的代码(上面的CPT版本),好的,最好将第一个代码块添加到函数中。主题(或子主题,如果您正在使用的话)的php。这是:

function get_all_them_posts(){
    $count_posts = wp_count_posts();

    $published_posts = $count_posts->publish;
    return $published_posts;
}
然后,如果希望获得模板中的帖子总数,请替换:

<?php echo $query->found_posts; ?> 
使用:

<?php echo get_all_them_posts(); ?>
该行将调用添加到函数中的函数。php通过这样做,您可以在其他模板文件中使用它,而无需每次都重写该函数。我希望这有帮助!

SO网友:Sam

$wp\\u query->$found\\u posts将在您过滤后更改,因为插件可能会更改查询,所以found\\u post每次都会更改。

$wp_query->$post_count
// The number of posts being displayed.
$wp_query->$found_posts
// The total number of posts found matching the current query parameters

// Total post amount in database
$count_posts = wp_count_posts();
// Total post amount in database of custom post type called cars
// $count_posts = wp_count_posts(\'cars\');
// Total count of the published amount of above
$published_posts = $count_posts->publish;
下面的代码应该是

if ( $query->have_posts() )
{
    ?>
<ul id="florefs">
    <?php
    while ($query->have_posts())
    {
        $query->the_post();

        ?>

        <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><i class="x-icon florex x-icon-angle-right" data-x-icon="" aria-hidden="true"></i>&nbsp;<?php the_field(\'br_name\'); ?></a>
        <div id="flosex"><span class="fimi"><?php the_field(\'br_category\'); ?></span><span class="flag <?php echo strtolower(get_field(\'br_heritage\')); ?>"></span><span class="fama"><?php the_field(\'br_heritage\'); ?></span></div></li>

        <?php
    }
    ?>
</ul>
    <div class="filhead">Page <?php echo $query->query[\'paged\']; ?> of <?php echo $query->max_num_pages; ?></div>

    <div class="pagination">

        <div class="nav-next"><?php previous_posts_link( \'<i class="x-icon x-icon-arrow-left" data-x-icon="" aria-hidden="true"></i> Previous page\' ); ?></div>
        <div class="nav-previous"><?php next_posts_link( \'Next page <i class="x-icon x-icon-arrow-right" data-x-icon="" aria-hidden="true"></i>\', $query->max_num_pages ); ?></div>

        <?php
            /* example code for using the wp_pagenavi plugin */
            if (function_exists(\'wp_pagenavi\'))
            {
                echo "<br />";
                wp_pagenavi( array( \'query\' => $query ) );
            }
        ?>
    </div>

<div id="prasti"><span class="prase">PROBLEM HERE</span><span class="praso"><?php echo wp_count_posts(); ?></span><span class="prasif">NEEDS</span><span class="prasi">CHANGE</span></div>

    <?php
}
else
{
    echo "There are no results for your selected criteria.";
}
?>

结束

相关推荐

似乎无法按当前类别ID筛选wp_Query

我正在查询一个自定义帖子类型(“捐赠者”),并希望按类别(此帖子类型的自定义分类法)列出它们,类别标题是该类别的所有帖子。经过大量研究,我得到了以下信息(在我定制的帖子类型的归档模板archive provider.php中):global $paged; global $query_args; $category_args = array( \'taxonomy\' => \'donor_taxonomy\', \'orderby\' =>