It’s not clearly stated in your question, but I guess, you added exactly this code to the widget file:
<?php echo $wp_query->current_post +1; ?>
Am I right?
If so, then it couldn’t work, because this code takes the counter from global $wp_query
object, but… This widget doesn’t use global $wp_query
and uses custom WP_Query
instance. You can see it clearly here: https://github.com/WordPress/WordPress/blob/master/wp-includes/widgets/class-wp-widget-recent-posts.php#L70.
And even worse (in this case), because this widget doesn’t use the_post
method and standard way of looping through posts, but native PHP foreach
loop (https://github.com/WordPress/WordPress/blob/master/wp-includes/widgets/class-wp-widget-recent-posts.php#L94), to be more efficient. But this also means that this loop doesn’t change the counter value…
But… There is one more, major, but.
You should never ever modify core WP files. If you do this, you won’t be able to update your WP or you will loose these modifications. You can easily cause any plugin or theme to work incorrectly.
So…? Is that it? It can’t be done?
Well, it can. If all you want to do is to add numbers in the li
element, you can do it with CSS.
One way would be to change the list-style-type
to decimal
for list in that widget:
.widget_recent_entries ul {list-style-type: decimal;}
Another way is to use :before
pseudo-class with counter:
.widget_recent_entries ul {
counter-reset: my-recent-posts-widget-counter;
}
.widget_recent_entries ul li {
counter-increment: my-recent-posts-widget-counter;
}
.widget_recent_entries ul li {
content: counter(my-recent-posts-widget-counter);
}