0

I'm using query_posts( 'offset=1' ); to exclude the first post from the loop. However, if the first post is a sticky post it will be shown and the second (non-sticky) post will not be shown.

Is there any method the first post will be hidden, no matter if it's sticky or not? I can't use ignore_sticky_posts because I only want to hide the first one.

Thanks for your help!

3
  • 2
    Just curious but why would you need something like this? Also, don't use query_posts.
    – webtoure
    Commented Aug 17, 2015 at 14:20
  • I want to style the first post differently and, since I'm using a masonry layout, can't do it with just CSS.
    – Stefan
    Commented Aug 17, 2015 at 14:34
  • So you need the rest of the elements to be positioned relative to this post, right? Perhaps the stamp method is useful. This may also be good to know in general.
    – webtoure
    Commented Aug 17, 2015 at 14:46

1 Answer 1

3

You shouldn't really need to "exclude" anything. Just make use of the built in loop tracking. WP_Query will "remember" where you left off. For example:

if (have_posts()) {
  while (have_posts()) {
    the_post();
    echo 'firstpost##';
    the_content();
    echo '##endfirstpost';
    break;
  }
}

// do some other stuff

if (have_posts()) {
  while (have_posts()) {
    the_post();
    echo 'otherposts##';
    the_content();
    echo '##endotherposts';
    break;
  }
}

Or just roll the code into the same loop:

if (have_posts()) {
  while (have_posts()) {
    the_post();
    if (0 == $wp_query->current_post) {
      echo 'firstpost##';
    else {
      echo 'otherposts##';
    }
    the_content();
    if (0 == $wp_query->current_post) {
      echo '##endfirstpost';
    else {
      echo '##endotherposts';
    }
    break;
  }
}
1
  • Thank you so much, I really didn't know this. This helps a lot!
    – Stefan
    Commented Aug 17, 2015 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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