Wordpress显示在自定义帖子类型中发布了多少篇文章

时间:2016-09-10 02:29:24

标签: php wordpress

我有一个简单的问题,但似乎没有正确显示我的查询。我想在我的自定义帖子“简单下载监视器”中设置帖子数量,以便我可以为我的文章类设置条件。这是我的简单代码。

代码图1

<?php $io_articles = new WP_Query( ['post_type' => 'sdm_downloads', 'posts_per_page' => -1, 'order' => 'desc'] ); ?>
<div class="section-io-list-articles">
<?php 
if ( $io_articles->have_posts() ) : 
  $count = wp_count_posts()->publish;
  if ( $count >= 3 ) {
    $class_column = 'column-3';
  } 
  elseif ( $count == 2 ) {
    $class_column = 'column-2';
  }
  ?>
  <div class="section-content <?php echo $class_column; ?>">
    <div class="row">
    <?php 
      while ( $io_articles->have_posts() ) : $io_articles->the_post();
      ?>
      <article class="column"> <?php the_title(); ?> </article>
      <?php 
      endwhile;
    ?>
    </div>
  </div>
  <?php 
  endif; wp_reset_postdata();
?>
</div>
如果我回应它,​​

$count通常会显示1而不是超过1个帖子。这样,我就有$count的特定类集来支持我的文章列,您可以在其中查看条件。

注意:我这里有4个帖子,所以它应该显示4作为$count的数字,它应该将列类设置为class="section-content column-3"。< / p>

1 个答案:

答案 0 :(得分:0)

我只是错过了wp_count_posts($ type)的用法。所以代码应该是这样的。

$count = wp_count_posts( 'sdm_downloads' )->publish;
if ( $count >= 3 ) {
  $class_column = 'column-3';
} 
elseif ( $count == 2 ) {
  $class_column = 'column-2';
}
相关问题