treehouse : what would you like to learn today?
Web Design Web Development iOS Development

multiple loop issue - previous posts page is wrong!?

  • hi!

    i have been trying to do something similar to css-tricks in showing my newest post in a different way to the other posts on the page. that seems all good, however when you click on "older posts" it has a problem as it still shows the newest post on each page, and doesn't work in the same way css-tricks does.

    could someone help me on this one please? i would really appreciate it.

    this is the site i have the problem on (i know its crap but i am trying!) http://www.mariosuniverse.com/blog

    here is my code... (thanks in advanced)

    do i need a conditional statement to only do it on the home page like <?php if (is_home()) { } ?> ??? if so can someone show me the way? cheers



    <?php get_header(); ?>
    <div class=\"top\"></div>
    <div id=\"main-content\">

    <div class=\"loop\">

    <?php $my_query = new WP_Query('category_name=featured&posts_per_page=1');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;?>

    <div id=\"datebox\">
    <p class=\"day\"><?php the_time('d') ?></p>
    <p class=\"month\"><?php the_time('M') ?></p>
    <p class=\"year\"><?php the_time('Y') ?></p>
    </div>
    <div class=\"star\"></div>
    <div class=\"clear\"></div>

    <div id=\"post-<?php the_ID(); ?>\"> </div> <!-- did wierd stuff with this when i moved \"loop\" up! -->
    <h2><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php the_title_attribute(); ?>\"> <?php the_title(); ?></a></h2>
    <?php the_content('Read The Article / Have your say!'); ?>

    <!-- <p><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> |
    <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> </p> -->

    </div>
    <br/>
    <br/>
    <div class=\"shroom\"></div>
    <br/>
    <br/>
    <br/>


    <?php endwhile; ?>

    <?php if (have_posts()) : while (have_posts()) : the_post();
    if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>

    <div id=\"datebox\">
    <p class=\"day\"><?php the_time('d') ?></p>
    <p class=\"month\"><?php the_time('M') ?></p>
    <p class=\"year\"><?php the_time('Y') ?></p>
    </div>
    <div class=\"star\"></div>
    <div class=\"clear\"></div>

    <div class=\"post\" id=\"post-<?php the_ID(); ?>\">
    <h2><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php the_title_attribute(); ?>\"> <?php the_title(); ?></a></h2>
    <?php the_content('Read The Article / Have your say!'); ?>
    <!-- <p><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p> -->


    <br/>
    <br/>
    <div class=\"shroom\"></div>
    <br/>
    <br/>
    <br/>
    </div>
    <?php endwhile; ?>

    <ul>
    <li><?php next_posts_link('&laquo; Older Entries') ?></li>
    <li><?php previous_posts_link('Newer Entries &raquo;') ?></li>
    </ul>

    <?php else : ?>

    <h2>Not Found</h2>
    <p>Sorry, but you are looking for something that isn't here.</p>
    <?php include (TEMPLATEPATH . \"/searchform.php\"); ?>

    <?php endif; ?>

    </div>

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>
  • Hiya,

    Just wanted to bump this again. (I won't keep doing it and piss everyone off).

    I have searched for ages for a fix on this but have still not found the info i am looking for, can anyone help?

    Thanks.
  • It is a fine question, and I feel like I know the answer, but it isn't coming to me at the moment.

    I'll ponder it some more, but hopefully Chris catches this one first!
  • thanks! any help would be gratefully recieved!!
  • ok so i have been on a wordpress forum and got this info...

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if ($paged < 2) {
    // DO STUFF
    }
    ?>

    The guy basically said that this would say that this would check if it was page 1, and do stuff accordingly. I had a quick go and couldn't work out how to use it with 2 loops because my knowledge is pretty lacking!

    what do you think Doc? or anyone else that fancies being my hero!?!!?

    Thank you!
  • Yeah you gotta check if you are on a paginated page for that first query. Otherwise that first query has no idea and will just grab the most recent post. You don't need to do that fancy stuff to know if a post is paginated though, you can just:

    if ($paged) {
    // do stuff
    }


    The $paged variable is equal to the # of page you are on. So if you are at /page/2, $paged will be 2. You can use that number to offset the query. Like

    if ($paged) {
    query_posts(\"posts_per_page=1&offset=$paged\")
    }


    That offset parameter will skip the first X posts. You'll need to adjust as needed. As in, if you show 5 posts per page, you'll need to offset by 6 on page two, so you'll need to do like:

    $offset = (5 * $paged) + 1;


    All covered in Digging Into WordPress -- wink wink.
  • got similar issue on my blog ... http://makeyourtheme.com/blog it just doesn't work here is my codes
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if ($paged < 2) {
    query_posts(\"posts_per_page=1&offset=(3 * $paged) + 1\");
    }
    ?>

    <?php query_posts('&cat=-4&posts_per_page=3'); ?>

    <?php if (have_posts()): ?>

    <?php while (have_posts()): the_post();?>

    <div <?php post_class() ?> id=\"post-<?php the_ID();?>\">
    <div class=\"latest\">
    <div class=\"blogtitle\"><h2><a href=\"<?php the_permalink() ?> \"><?php the_title();?></a></h2></div>

    <p class=\"dateposted\">Posted on: <?php the_time('l, F jS, Y') ?> at <?php the_time() ?></p>
    <p class=\"author\">by:<?php the_author(); ?></p>
    <hr />
    <div class=\"excerpt\"><?php the_excerpt();?></div>
    </div>

    </div>

    <?php endwhile; ?>
    <?php next_posts_link('&laquo; Older Entries') ?>
    <?php previous_posts_link('Newer Entries &raquo;') ?>
    <?php else: ?>

    <?php endif;?>


    hope someone can help. . . i got the dig-wp book btw but dont know which chapter to get around this problem is it page 95?
  • Here's my code...
    I have the first post looking different ONLY on the first page... I use pagenavi for pagination.


    <?php // Beginning of the 1st page ?>
    <?php if ( $paged < 2 ) { ?>
    <?php if (have_posts()) : ?>
    <?php $count = 0; ?>
    <?php query_posts($query_string . '&cat=-3,-5,-6,-8,-11'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $count++; ?>
    <?php if ($count <= 1) : ?>
    <div class=\"latest-post\"><h2><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php the_title_attribute(); ?>\"><?php the_title(); ?></a></h2>
    <div class=\"date\"><span class=\"month\"><?php the_time('M') ?></span><span class=\"day\"><?php the_time('j') ?></span><span class=\"year\"><?php the_time('Y') ?></span></div>
    <div class=\"thumbnail\"><a href=\"<?php the_permalink() ?>\"><?php the_post_thumbnail('latest'); ?></a></div>
    <div class=\"entry\"><?php the_excerpt(); ?><div class=\"read-more\"><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php the_title_attribute(); ?>\">read more &gt;</a></div>
    </div><div class=\"clear\"></div><div class=\"clear\"></div></div><!-- end LATEST POST -->
    <?php else : ?>
    <div class=\"post\">
    <div class=\"date\"><span class=\"month\"><?php the_time('M') ?></span><span class=\"day\"><?php the_time('j') ?></span><span class=\"year\"><?php the_time('Y') ?></span></div>
    <div class=\"thumbnail\"><a href=\"<?php the_permalink() ?>\"><?php the_post_thumbnail(); ?></a></div>
    <h2><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php the_title_attribute(); ?>\"><?php the_title(); ?></a></h2>
    <div class=\"entry\"><?php the_excerpt(); ?>
    <div class=\"read-more\"><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php the_title_attribute(); ?>\">(...)</a></div>
    </div>
    <div class=\"clear\"></div>
    </div><!-- end POST -->
    <?php endif; ?>
    <?php endwhile; ?>
    <?php wp_pagenavi(); ?>
    <?php else : ?>
    <h2>Not Found</h2>
    <p>Sorry, but you are looking for something that isn't here.</p>
    <?php endif; ?>

    <?php //This is the dividing line - Above: First page of the blog / Below: All the other pages ?>
    <?php } else { ?>
    <?php if (have_posts()) : ?>
    <?php query_posts($query_string . '&cat=-3,-5,-6,-8,-11'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class=\"post\">
    <div class=\"date\"><span class=\"month\"><?php the_time('M') ?></span><span class=\"day\"><?php the_time('j') ?></span><span class=\"year\"><?php the_time('Y') ?></span></div>
    <div class=\"thumbnail\"><a href=\"<?php the_permalink() ?>\"><?php the_post_thumbnail(); ?></a></div>
    <h2><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php the_title_attribute(); ?>\"><?php the_title(); ?></a></h2>
    <div class=\"entry\"><?php the_excerpt(); ?>
    <div class=\"read-more\"><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php the_title_attribute(); ?>\">(...)</a></div>
    </div>
    <div class=\"clear\"></div>
    </div><!-- end POST -->
    <?php endwhile; ?>
    <?php wp_pagenavi(); ?>
    <?php else : ?>
    <h2>Not Found</h2>
    <p>Sorry, but you are looking for something that isn't here.</p>
    <?php endif; ?>
    <?php } ?>
    <?php // end the 1st & 2nd page code ?>

  • I need some help with this too...links keep reverting back to the new entries and not the older ones.

    I've been adding the suggested code above from Chris into my bloghome.php file but its not happening for me.
    blog page is http://vigourdynamicfitness.com.au/blog/....can anyone lend a hand =D