Blog

【WordPress】投稿やカスタム投稿の一覧ページで「全●件/●件~●件」を表示する方法

2020/12/14

投稿 カスタム投稿 Wordpress

備忘録です。

以前、「総ページ数と現在のページを取得する方法」という記事を書きましたが、
今回は投稿などの一覧ページで「全●件/●件~●件」を表示する方法です。

参考サイト
WordPress で「全◯件中◯件〜◯件目を表示」を表示する

functions.phpに記述

//投稿の記事数を表示
function my_result_count() {
  global $wp_query;

  $paged = get_query_var( 'paged' ) - 1;
  $ppp   = get_query_var( 'posts_per_page' );
  $count = $total = $wp_query->post_count;
  $from  = 0;
  if ( 0 < $ppp ) {
    $total = $wp_query->found_posts;
    if ( 0 < $paged )
      $from  = $paged * $ppp;
  }
  printf(
    '<p>全%1$s件中 %2$s%3$s件目を表示</p>',
    $total,
    ( 1 < $count ? ($from + 1 . '〜') : '' ),
    ($from + $count )
  );
}

archive.phpなどの一覧を表示するテンプレートに記述

<?php if ( have_posts() ) : 
  my_result_count();  // ここら辺で表示します
  while ( have_posts() ) :
    the_post();
    /* do stuff */
  endwhile;
else :
  /* Nothing Found */
endif; ?>

カテゴリー

月間アーカイブ

MORE

ミュージシャンズ・プラザ

神社仏閣ホームーページ制作

ホームページ制作問合せ