Domanda

Not sure if I'm using the proper terminology in my title so feel free to correct.

Say I have a custom post type called colors with say 40 posts that are in the default order in single_colors.php I have:

$my_query = new WP_Query(
 array(
  'post_type' => 'colors',
  'posts_per_page' => 5,
  'offset' => 2
 );
if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile;  ?>
<?php endif; wp_reset_query(); ?>

That would echo 10 posts from the custom post type starting with the 2nd in the list of available posts.

I'd like to put the post number before each title. So in the above example would then echo:

2 Red 3 Blue 4 Green 5 Black 6 Yellow

I was able to add a count feature but no matter the offset it would start the count at 1. Is there a variable, function or something to accomplish this?

È stato utile?

Soluzione

In your case, count would be offset + current_post:

$my_query = new WP_Query(
    array(
        'post_type' => 'colors',
        'posts_per_page' => 5,
        'offset' => 2
    )
);
if ( $my_query->have_posts() ) :
    while ( $my_query->have_posts() ) :
        $my_query->the_post();
        echo $my_query->query_vars['offset'] + $my_query->current_post;
        the_title();
    endwhile;
endif;
wp_reset_postdata();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top