0

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?

1

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();
  • Brillant! That worked. Glad I was on the right path, but you were definetly able to put it all together in a clean simple way. – cchiera Sep 14 '17 at 11:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.