• Resolved belledumonde

    (@belledumonde)


    I have two loops running on my homepage. The on top uses this plugin

    https://mattread.com/projects/wp-plugins/custom-query-string-plugin/

    to show all posts in one day. Then the one on the “bottombar” shows the excerpts of the next 3 with an offset of 1. This is all fine except for when I have more than one post in one day. The excerpts on the bottom still show the second post of the same day.

    Is there a way to get around this? Is it possible to put a counter on the top loop and use this number to set as the offset number in the second separate loop at the bottom? Any other ways?

    Thank you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Is it possible to put a counter on the top loop and use this number to set as the offset number in the second separate loop at the bottom?

    This sounds like the simplest method you could use, since it avoids having to query the database or anything like that, but mostly because you want to feed off the first loop anyway.

    So you could add a variable that increments for each post *somewhere* in the first loop:

    <?php $dayposts++; ?>

    then pass $dayposts as the value of your offset.

    PHP note: Make sure to use double-quotes for your parameter string rather than single-quotes (otherwise PHP will interpret the variable literally, i.e. $dayposts rather than 2). So:

    ("offset=$dayposts")

    Thread Starter belledumonde

    (@belledumonde)

    oh goodie, so i put <?php $dayposts++; ?> as is somewhere in the first loop? and =$dayposts as my value for the offset? so it’s really simple, thanks! but um, where exactly is somewhere? After the if? while? doesn’t matter where?

    thank you ??

    The Loop logically starts here >>
    while(have_posts()) :

    Anything placed from here on, before the line that ends the while loop (i.e. endwhile;) will repeat, or in our case increment. But in general:

    Loop Start >>
    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>

    ~In The Loop~

    <?php endwhile; endif; ?>
    << Loop End

    Thread Starter belledumonde

    (@belledumonde)

    Okie dokie so let me just clarify so I know what to do, if this my basic loop:

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    <h2><?php the_title(); ?></h2>
    <div class="entry"><?php the_content(); ?></div>
    </div>
    <?php endwhile; endif; ?>

    I insert <?php $dayposts++; ?> under <?php while (have_posts()) : the_post(); ?> so it becomes

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $dayposts++; ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    <h2><?php the_title(); ?></h2>
    <div class="entry"><?php the_content(); ?></div>
    </div>
    <?php endwhile; endif; ?>

    ??

    UDPATE: I just tried the above and it doesn’t seem to be working. Maybe I’m doing the bit in the second loop wrong. These are the things I’ve tried so far.

    <?php query_posts('offset="$dayposts"&showposts=3'); ?>
    <?php query_posts('"offset=$dayposts"&showposts=3'); ?>

    <?php query_posts("offset=$dayposts&showposts=3"); ?>

    Thread Starter belledumonde

    (@belledumonde)

    Ok, it’s uh… Still not working. Do these two loops have to be in the same file? Because at the moment I have the first loop in home.php and the second one with <?php query_posts("offset=$dayposts&showposts=3"); ?> in my sidebar.php

    Oh it does, I just tried it. Ok thank you! ??

    Note to future readers: On the off chance you are using a variable of your own making (as here) in two or more templates but it fails to work, scope the variable to global in all templates it occurs. For example:

    home.php >>
    <?php global $dayposts; ?>
    <?php if (have_posts()) : ?>
    ...

    sidebar.php >>
    <?php
    global $dayposts; query_posts("offset=$dayposts&showposts=3");
    ?>

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘offset=1day?’ is closed to new replies.