admin管理员组文章数量:1023764
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
2 Answers
Reset to default 0You 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.
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
2 Answers
Reset to default 0You 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.
本文标签: countGet post number both in the loop and in the post
版权声明:本文标题:count - Get post number both in the loop and in the post 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745589861a2157821.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论