WordPress.org

Ready to get started?Download WordPress

Forums

How to offset a loop prior to specifying a category (6 posts)

  1. amioni
    Member
    Posted 1 year ago #

    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?

  2. jnhghy
    Member
    Posted 1 year ago #

    Hi,
    replace your query with:
    <?php query_posts('posts_per_page=6&offset=3'); ?>
    More info here.
    Regards

  3. amioni
    Member
    Posted 1 year ago #

    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.

  4. jnhghy
    Member
    Posted 1 year ago #

    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();
     endwhile;
     $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...)
    Regards

  5. amioni
    Member
    Posted 1 year ago #

    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.

  6. jnhghy
    Member
    Posted 1 year ago #

    "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;

Topic Closed

This topic has been closed to new replies.

About this Topic