count - Get post number both in the loop and in the post

I have a post type that is essentially a photography zine. I want each post to display in the archive page as an "i

I have a post type that is essentially a photography zine. I want each post to display in the archive page as an "issue" with a number associated, from oldest to newest. The first issue would be "issue 1" while the tenth issue would be "issue 10". I need this display both on the archive page and also on the post page itself.

Here is the code I was using

<?php 
  echo $wp_query->found_posts - $wp_query->current_post ;

But this only seems to work on the archive page.

I have a post type that is essentially a photography zine. I want each post to display in the archive page as an "issue" with a number associated, from oldest to newest. The first issue would be "issue 1" while the tenth issue would be "issue 10". I need this display both on the archive page and also on the post page itself.

Here is the code I was using

<?php 
  echo $wp_query->found_posts - $wp_query->current_post ;

But this only seems to work on the archive page.

Share Improve this question edited Apr 14, 2019 at 15:48 Nathan Johnson 6,5286 gold badges30 silver badges49 bronze badges asked Apr 14, 2019 at 0:50 Garrett ScafaniGarrett Scafani 731 silver badge6 bronze badges 2
  • Is there any code, that you could share, showing what you've tried yourself this far? E.g. your post type archive template or single template? – Antti Koskinen Commented Apr 14, 2019 at 12:23
  • whoops, yes. I edited my original question to include a snippet. – Garrett Scafani Commented Apr 14, 2019 at 14:50
Add a comment  | 

2 Answers 2

Reset to default 0

You can do it like this on single post page following this answer. The code may need some tweaking to meet your exact requirements.

Put this in functions.php of your theme

 class MY_Post_Numbers {

  private $count = 0;
  private $posts = array();

  public function display_count() {
      $this->init(); // prevent unnecessary queries
      $id = get_the_ID();
      echo sprintf( '<div class="post-counter">Post number<span class="num">%s</span><span class="slash">/</span><span class="total">%s</span></div>', $this->posts[$id], $this->count );
  }

  private function init() {
      if ( $this->count )
          return;
      global $wpdb;       
      $posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date " ); // can add or change order if you want 
      $this->count = count($posts);

      foreach ( $posts as $key => $value ) {
          $this->posts[$value] = $key + 1;
      }
      unset($posts);
  }

}

$GLOBALS['my_post_numbers'] = new MY_Post_Numbers;

function my_post_number() {
  $GLOBALS['my_post_numbers']->display_count();
}

to use in template file :

<?php my_post_number(); ?>

Using API function is an option, and in my opinion generally preferable. As counting, numbering on the archive page isn't an issue, this concerns how to do it at the view of a single post. This is not ready to use code, just an exemplary outline:

$current_posts_id = get_the_ID();
$wp_query_obj = new WP_Query(
    [
        //other conditions to determine order
        //use the same as for the archive page, otherwise the numbering differs

        //use parameter fields to get an array of
        //keys numerically indexed, with value post id
        'fields' => 'ids'
    ]
);
//get the numerically array of post ids into its own variable
$wp_query_posts_array = $wp_query_obj->posts;
//search array by value, which is the post id, and return corresponding key, numeric index
$wp_query_posts_array_index = array_search( $current_posts_id, $wp_query_posts_array );
//the array index starts with 0, add one for the issue numbering
$current_posts_issue_number = $wp_query_posts_array_index + 1;



Note: This is of course, if you want to get the issue number on the fly. Generally speaking, if you want or have to work with the issue number more frequently, or want to do queries where you can use the issue number, I probably would introduce a post meta to store the issue number. But that's just my thought in regard to possibly necessary code, data structure design choices.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745589863a4634766.html

相关推荐

  • count - Get post number both in the loop and in the post

    I have a post type that is essentially a photography zine. I want each post to display in the archive page as an "i

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信