Support » Fixing WordPress » How to offset a loop prior to specifying a category

  • I have a loop for one category that I want to offset by 3 posts. However, I want the offset to apply to all the posts on my site, not just the ones from that category.

    Here is my loop:

    <?php query_posts('posts_per_page=6&cat=12&offset=3'); ?>

    This works fine to offset by 3 posts in category 12, but that’s not what I want. I want it to offset by 3 of my posts no matter what category they are in.

    Can anyone help with this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,
    replace your query with:
    <?php query_posts('posts_per_page=6&offset=3'); ?>
    More info here.

    Thanks for your quick response. But I do want this loop to be only for category 12. I just want the offset to be for all the posts.

    In other words, I want to skip the first 3 posts regardless of their category, and then show the next 6 posts only from category 12.

    Sorry I was in a hurry to help and haven’t waited to read the details of your request.

    <?php query_posts('posts_per_page=3');
     while ( have_posts() ) : the_post();
           $to_exclude[] = get_the_ID();
     $to_exclude[0] = "-".$to_exclude[0];
     $to_exclude = implode(",",$to_exclude);
     query_posts('posts_per_page=6&cat=12&p=$to_exclude'); ?>

    This should get the ids of the first 3 posts regardless of their category and then query for the next posts from cat 12… hope it works (haven’t tested it so if you get an error and can’t fix… let me know…)

    I didn’t get an error, but it didn’t change anything. I get the exact same results with your code as I got with my original code.

    Your idea of getting the IDs of the first 3 posts and then excluding them that way sounds very promising. But I don’t know enough PHP to see why it doesn’t work. Is the problem in the last line: p=$to_exclude. I don’t get what the “p” refers to.

    “p” is for post id and I tried to use p=-1 for example to exclude the post with the id = 1 you’ll probably have to var_dump or to echo the variables to see which is not getting the desired value;

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to offset a loop prior to specifying a category’ is closed to new replies.