2016-04-19 9 views
0

に現在のポスト番号(ないID)を取得し、私は記事の量を取得するには、この機能を使用しています:ワードプレス

$postAmount = wp_count_posts('post')->publish; 

これは右の数である、4を返します。 しかし、現在の投稿番号を確認できる機能もありますか? IDではなく数字だけです。

たとえば、私は2番目のポストにあるので、関数は '2'を返すようにします。

追加情報

$wp->query->current_post+1 0を返す全ての投稿

答えて

2

であなたは右のアイデアを得たが、ちょうど小さなタイプミスを作りました。何が欲しいのは、あなたがゼロから始まるカウントする天気をかに応じ

$wp_query->current_post 

または

$wp_query->current_post + 1 

です。

enter image description here

代わりに$ wp-> query-> current_post + 1

のような完全なコードのWP_Queryコーデックスhttps://codex.wordpress.org/Class_Reference/WP_Query

+0

オハイオ州のハハは、誤植でした。ありがとう! –

0

使用$ wp_query-> current_post + 1でcurrent_postを検索..

<?php 

$postArg = array('post_type'=>'post', 
         'posts_per_page'=>-1, 
         'order'=>'desc', 
        ); 
     global $post; 
     $getPost = new wp_query($postArg); 
      if($getPost->have_posts()){ 
      echo '<ul>'; 
       while ($getPost->have_posts()):$getPost->the_post(); 

        $index= $getPost->current_post + 1; 
        echo "<h2>".$post->post_title."</h2>".$index; 

       endwhile; 
      echo '</ul>'; 
     } 

?>