• is it not possible to state an offset for query_posts?

    i want to have my first post be styled one way, the second another, and the rest a third way.

    offset was a feature of get_posts, do i need to still use get_posts in order to do this? any ideas?

Viewing 15 replies - 1 through 15 (of 19 total)
  • Hmm. I’m pounding on this and the best I can come up with is a play between paged and posts_per_page. The first post would use:

    query_posts('paged=1&posts_per_page=1');

    The second:

    query_posts('paged=2&posts_per_page=1');

    The third (and rest):

    query_posts('paged=3&posts_per_page=N');

    N being the number of posts to diplay for the rest. Note this is not working as intended. Still pounding…

    Ah, this is exactly the problem I was just about to post about!

    Any ideas would be welcome πŸ™‚

    yanis

    (@yanis)

    I had a similar problem as well.. there is another post here somewhere that outlines the following (which works perfectly):

    <?php $posts = get_posts('numberposts=10&offset=1'); foreach ($posts as $post): setup_postdata($post); ?>

    This resets the counter, and re-runs the loop. The offset can be set to whatever you want, the default is 0.

    In my case, I run with two .php pages, home.php and then a second .php (in my case content.php) The first home.php is a copy of the index.php, with the following code snippet to restrict WP to display only 1 post, the latest, just below if (have_posts())

    query_posts('showposts=1')

    It works nicely, and avoides chweing index.php to bits.

    Any solution for this offset thingy? I want on the home.php to have one post from a certain category on the top (different display) and after that displaying the 5-6 older posts form all categories – with another query, but EXCLUDING the one it was already displayed.

    moshu, you don’t need an offset for that but rather an ability to exclude a category from the second query, which is easily done. An example of your two loops (assuming cat ID #1):

    Loop #1:
    <?php $top_query = new WP_Query('cat=1&showposts=1'); ?>
    <?php while($top_query->have_posts()) : $top_query->the_post(); ?>

    ~ template tags 'n stuff go here ~

    <?php endwhile; ?>

    Loop #2:
    <?php query_posts('cat=-1&showposts=5'); ?>
    <?php while(have_posts()) : the_post(); ?>

    ~ template tags 'n stuff go here ~

    <?php endwhile; ?>

    Thanks, Kafkaesquí, I got that. My problem is, that among those “5” in the second Loop I do want to have posts from cat=1, but not the last one (showed in Loop #1)
    Is that possible?

    Hmm…

    What you might do is grab the post ID from that first post, and use it to test against for displaying in the second loop. So your mod to the first:

    <?php while($top_query->have_posts()) : $top_query->the_post(); $first_post = $post->ID; ?>

    Then the new second loop:

    <?php query_posts('showposts=6'); ?>
    <?php while(have_posts()) : the_post(); if(!($first_post == $post->ID)) : ?>

    ~ template tags 'n stuff go here ~

    <?php endif; endwhile; ?>

    EDIT: I changed the showposts value, based on the idea the first post will probably be within those, and stripping it out would lessen the # of posts by one. But this is one of those assumptions it may be better to clear up. For example, you could set up an incremental counter to match against the *true* number of posts to display in the second loop, and end things when it reaches that value.

    Thank you so much! It works perfectly. You made my day πŸ™‚

    yes thanks a _lot_. I’ve been searching for days…i just didn’t think to look in the support forum…how odd.

    here is how I implimented it:
    http://dontcom.com/

    I’m looking for an answer to a similar question, so I thought I’d post here instead of starting a new thread.

    I’d like to split my loop into two divs, with 3 posts in each div. I have acheived this by using query_posts for the first loop and get_posts (which has an offset parameter) for the second. However, I would also like to exclude a certain category from both these loops, but get_posts doesn’t offer that option.

    Basically, what I’m asking is, is there a way to offset a query_posts loop by 3, so I can use 2 query_post loops instead of 1 query_posts and one get_posts?

    I’m a noob to most of this stuff, so if I’m being stupid, or if this has already been answered above (I don’t think it has), please be gentle.

    Hi,

    I’d like to split my loop into two divs, with 3 posts in each div. I have acheived this by using query_posts for the first loop and get_posts (which has an offset parameter) for the second. However, I would also like to exclude a certain category from both these loops, but get_posts doesn’t offer that option.

    maybe my solution solves the problem for other users as well…
    I’ve solved the problem shown above for me in that way:
    my first loop looks like this:

    <?php $my_query = new WP_Query('showposts=3');
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <!-- Show the first three posts in div1 -->
    <?php endwhile; ?>

    and the second loop:

    <?php
    $my_query = new WP_Query('showposts=6'); // showposts here minus the showposts from the first loop will be displayed
    while ($my_query->have_posts()) : $my_query->the_post();
    $seconddiv = $post->ID;
    if($seconddiv > 6){
    ?>
    <!-- Show the next three posts in div2 -->
    <?php endwhile; ?>

    …maybe quick & dirty – but it works. πŸ˜‰

    greets,
    Marc πŸ™‚

    I’m somewhat in the same situation myself – I would like to take a query and offset the results by 5 so that I get the *next* twenty posts in the list. This is the query I have written out right now, but it isn’t working:

    <div id="content">
    <div id="primary">

    <?php $my_query = new WP_Query('category_name=content&showposts=20&offset=5');
    while ($my_query->have_posts()) : $my_query->the_post();
    if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts);
    ?>
    <div class="postNormal" id="post-<?php the_ID(); ?>">
    <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h1>
    <div class="entry">
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_excerpt('Read the rest of this entry &raquo;'); ?></a></p>
    </div>
    </div>

    <?php endwhile; ?>

    Hi so far i have tried this it works good…
    But i’m using Category-#.php

    i want in each category instaid of excluding a category only get the lastest post from that category only!
    for example category-1.php

    i want the last post to look diffrent then the rest of the posts

    < Post1 >
    ….
    : post 2 :
    : post 2 :
    : post 2 :
    : post 2 :

    Only from Category1

    how can id o this?

    Hey, Kafkaesqui. I’ve tried to insert your code into my wordpress-index-file but I’ve only received some errors. Can you please explain it with some more details/instructions? Thanks a lot.

    I can almost, but not quite get it to work, using Kafkaesqui’s code. How do I modify the code for my “second” loop so that it displays whatever I queried it to, except for the post which has the same id as $first_post? This is my existing code (nevermind the $loopcounter which just helps keep track of odd and even posts):


    <?php query_posts('showposts=6'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();
    $loopcounter++; ?>

    This is Kafkaesqui’s code that I couldn’t get to work (and yes, I swapped the endif and endwhile after the loop in order to match this):


    <?php while(have_posts()) : the_post(); if(!($first_post == $post->ID)) : ?>

    I’ve already set the $first_post to be whatever id the first post has. So what I need is a way to melt those different code pieces into one, the reason I can’t quite get it to work is that I don’t know enough php to be able to express what I want to (or to swap the if-while logic of the former with the while-if logic of the latter).

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘query_posts offset’ is closed to new replies.