1

It works in the category.php loop

<article>
<p>post-<?php echo $wp_query->current_post +1; ?></p>
</article>

<article>
<p>post-<?php echo $wp_query->current_post +1; ?></p>
</article>

Result. Shows number 1, 2, 3 ...

<article>
<p>post-1</p>
</article>

<article>
<p>post-2</p>
</article>

Where to add this to widget recent posts?

If you add to the file /wp-includes/widgets/class-wp-widget-recent-posts.php

Result. Shows only number 1.

<ul>
<li>post-1</li>
</ul>

<ul>
<li>post-1</li>
</ul>

This is not a loop? How does this work in a widget?

$wp_query->current_post +1

class-wp-widget-recent-posts.php or functions.php?

Thanks.

1

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);
}
  • I added this code to the widget, but it did not work. I like the way with CSS. Thank you very much. – user158610 Jan 15 at 17:18
  • @Stanisław never modify core WP files. – Krzysiek Dróżdż Jan 15 at 17:20
-1

I didn't get what actually you are asking for ? Post count or Post ID

As i understand, i assume you want to show number of post list on widget named called recent posts.
Before i say something, going to default setting and checking is not good. Everything you can do that from your theme directory and also you should have information related to widgets api. For reference:
https://codex.wordpress.org/Widgets_API
From it, you can create your own widget like for recent posts and add number count to each post as you asked for.
for counting, you can use for loop.

if ( have_posts() ) :
    $count = 1;
      /* Start the Loop */
     while ( have_posts() ) :
       the_post();
        ?>
        <ul>
          <li>post-<?php echo $count; ?></li>
        </ul>
        <?php
       $count++; 
     endwhile;
endif;
  • OK, and why is using custom variable better than using counter that already exists in $wp_query object? – Krzysiek Dróżdż Jan 12 at 19:52
  • I just assume that, by your question, you are just beginning of WordPress. You can use count function that is already predefined. Its the alternative to count who wants to have easy and understandable code. – EGWorldNP Jan 13 at 17:35
  • Yeah, I’m a total noob in WP and PHP - great assumption ;) I asked that question, because you recommended some solution which is very redundant and ignores built in WP functionalities. That’s why I gave you a chance to explain it ;) – Krzysiek Dróżdż Jan 13 at 17:37
  • Thanks alot for giving chance. Actually, i am providing this loop in separate situation. I assume that you want to order your post with list of number like 1 : post tile – EGWorldNP Jan 15 at 6:26
  • 2 : post title 3 : post title and so on. For this you can use this loop. Nevermind, as your question made confusion and misunderstanding occurs. – EGWorldNP Jan 15 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy