WordPress.org

Ready to get started?Download WordPress

Forums

Mulitple Loops Specific Question (7 posts)

  1. khaled
    Member
    Posted 9 years ago #

    Yup, read up on the excellent entry about multiple loops found here:
    http://codex.wordpress.org/The_Loop

    I've just got a small question. I want to have the main post at the top and the 'featured' post below that. Would it be just a matter of moving the relevant code up, because then doesn't the

    if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>

    code not see the $do_not_duplicate post?

  2. IsaacSchlueter
    Member
    Posted 9 years ago #

    Try this:
    <?php
    $my_query = new WP_Query('category_name=featured&showposts=1');
    while ($my_query->have_posts()) : $my_query->the_post();
    $featured_post = $post; // save the feature post for later...
    endwhile; ?>
    <?php
    if (have_posts()) :
    while (have_posts()) :
    the_post();
    if( $post->ID == $featured_post->ID ) continue;
    update_post_caches($posts);
    ?>
    <!-- Do stuff with your regular posts... -->
    <?php
    endwhile;
    endif;
    if( $featured_post ):
    $post = $featured_post;
    ?>
    <!-- Do stuff with your featured post... -->
    <?php
    endif;
    ?>

  3. khaled
    Member
    Posted 9 years ago #

    Thanks Isaac, that looks good. I'll test it out later today.

  4. khaled
    Member
    Posted 9 years ago #

    okay I'm nearly there, just need an extra nudge in the right direction. Basically what's happening is that in the second loop the title is showing up right, but the content is actually the first entry in all the rest of the categories etc. So say I had 5 posts in total one in the featured category. The title of the featured category shows up in the place it's meant to but also the very first post as well.

    here's the code I'm using in the second loop, I'm sure I'm missing something fundamental here...


    <div id="primaryContent">

    <?php
    $my_query = new WP_Query('category_name=gallery&showposts=1');
    while ($my_query->have_posts()) : $my_query->the_post();
    $gallery_post = $post; // save the feature post for later...
    endwhile;
    ?>

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

    <div class="post" id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

    <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div> <!-- entry -->

    <p class="postmetadata">Posted in <?php the_category(', ') ?> <strong>|</strong> <?php edit_post_link('Edit','','<strong>|</strong>'); ?>
    <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    </div><!-- post -->

    <?php comments_template(); ?>
    <?php endwhile; ?>

    </div><!-- primaryContent -->
    <hr />

    <div id="gallery">

    <?php
    if( $gallery_post ):
    $post = $gallery_post;
    ?>

    <div class="post" id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?></small>

    <div class="entry">
    <?php the_content(''); ?>
    </div> <!-- entry -->

    </div><!-- post -->

    <?php endif; ?>
    </div><!-- gallery -->

  5. ifelse
    Member
    Posted 9 years ago #

    Khaled,

    Try the following:
    <div id="primaryContent">

    <?php
    $my_query = new WP_Query('category_name=gallery&showposts=1');
    while ($my_query->have_posts()) : $my_query->the_post();
    $gallery_post = $post; // save the feature post for later...
    endwhile;
    ?>

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

    <div class="post" id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

    <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div> <!-- entry -->

    <p class="postmetadata">Posted in <?php the_category(', ') ?> <strong>|</strong> <?php edit_post_link('Edit','','<strong>|</strong>'); ?>
    <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>

    </div><!-- post -->

    <?php comments_template(); ?>
    <?php endwhile; ?>

    </div><!-- primaryContent -->

    <?php endif; ?>
    <hr />

    <div id="gallery">

    <?php
    query_posts('category_name=gallery&showpost=1');
    if (have_posts()) :
    while (have_posts()) :
    the_post();
    ?>

    <div class="post" id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?></small>

    <div class="entry">
    <?php the_content(''); ?>
    </div> <!-- entry -->

    </div><!-- post -->
    <?php endwhile; ?>
    <?php endif; ?>
    </div><!-- gallery -->

    BTW, is this for BK4?

  6. khaled
    Member
    Posted 9 years ago #

    Phu you're the man. Yeah just getting things sorted out for it. I'll probably be posting a lot on the forums in the coming days, thanks for the heads up :)

  7. ifelse
    Member
    Posted 9 years ago #

    No problem:-) Glad I could help!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.