wordpress 如何从 query_posts 获取结果数量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11086315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:56:34  来源:igfitidea点击:

How to get number of results from query_posts?

wordpress

提问by iWizard

I'm printing posts and I want to get number of results, how can I do that?

我正在打印帖子,我想获得一些结果,我该怎么做?

This is part of my code:

这是我的代码的一部分:

if (have_posts()) : 

    $args = array(
        'showposts' => '5',
        'paged' => $paged
    );


    $thePosts = query_posts($args);
...

Thank's for help

感谢帮助

回答by iWizard

SOLVED:

解决了:

if (have_posts()) : 

        $args = array(
            'showposts' => '5',
            'paged' => $paged
        );


        $thePosts = query_posts($args);


         global $wp_query; 
         echo $wp_query->found_posts;
    ...

回答by Ryan B

To display the number of results of a search, use:

要显示搜索结果的数量,请使用:

Search Result for 

<?php 
/* Search Count */ 
$allsearch = &new WP_Query("s=$s&showposts=-1"); 
$key = wp_specialchars($s, 1);
$count = $allsearch->post_count; _e('');
 _e('<span class="search-terms">'); 
echo $key; _e('</span>'); 
_e(' &mdash; '); 
echo $count . ' ';
 _e('articles');
 wp_reset_query(); 
?>

This was taken from: WP Beginner.

这是取自:WP 初学者

回答by Gabriel Reguly

The correct answer is

正确答案是

 if (have_posts()) : 

    $args = array(
        'showposts' => '5',
        'paged' => $paged
    );


    $thePosts = query_posts($args);



     echo $thePosts ->found_posts;
...

回答by George

This will give you the results: Showing results 11-20 of 46, for instance.

这将为您提供结果:例如,显示 46 个结果中的第 11-20 个。

  $args = array(
    'cat'=> $cat,
    'posts_per_page' => 10,
    'paged' => $paged,
    's'=> $s
  );
  query_posts($args);

  $startpost=1;
  $startpost=10*($paged - 1)+1;
  $endpost = (10*$paged < $wp_query->found_posts ? 10*$paged : $wp_query->found_posts);
        ?>
  <h2 class="displayResult">Showing results <?php echo $startpost; ?> - <?php echo $endpost; ?> of <?php echo $wp_query->found_posts; ?></h2>

If this is not a search page, simply remove the line "'s'=> $s".

如果这不是搜索页面,只需删除该行"'s'=> $s"

If you do need it, make sure you declare the variable as $_GET['s']above.

如果确实需要它,请确保$_GET['s']如上所述声明变量。

回答by kaiser

Easy. To display number of results for this current page, use

简单。要显示当前页面的结果数,请使用

// Showing Page X of Y
print filter_var( absint( $GLOBALS['wp_query']->post_count ), FILTER_SANITIZE_NUMBER_INT );

For the total amount of results, use

对于结果总数,请使用

print filter_var( absint( $GLOBALS['wp_query']->found_posts ), FILTER_SANITIZE_NUMBER_INT );

回答by kdgilang

display numbers of search results :

显示搜索结果的数量:

<?php global $wp_query;
echo $wp_query->post_count; ?> 

回答by Giang D.MAI

query_posts( $args );
global $wp_query;
print_r($wp_query->max_num_pages);

It help me.

它帮助我。