谢谢Pieter。
基于this answer, 我对特定类别的工作做了一些更改。这是我的最终代码:
<div class="lessonNumber">
<?php
class MY_Post_Numbers {
private $count = 0;
private $posts = array();
public function display_count() {
$this->init(); // prevent unnecessary queries
$id = get_the_ID();
echo __(\'שיעור\', \'swgeula\') . \' \' . $this->posts[$id] . \' \' . __(\'מתוך\', \'swgeula\') . \' \' . $this->count;
}
private function init() {
if ( $this->count )
return;
$parent_cat = get_the_category()[0];
$parent_cat_id = $parent_cat->cat_ID;
global $wpdb;
$posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'post\' AND ID IN ( SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id = \'" . $parent_cat_id . "\' ) 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();
}
my_post_number();
?>
</div>