0

Is it possible to run an array to not show the first 6 posts, but then to show posts labeled 7-15 say?

So that way I can display the first 6 posts elsewhere and then the rest somewhere else?

More Information:

I have a site here: http://universitycompare.com/student-info and I would like the site to display the first 6 post's in images and then the rest as text below in the 'more articles' section.

Any more help needed to answer just let me know, the more control over this the better, so I would like to run them separately.

If you need to know my PHP that I am using see below: (I have left the HTML mark up in for the mean time so you know how i'm changing the style of it).

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

    <?php $the_query = new WP_Query( 'showposts=6' ); ?>
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

    <div class="si-biplace">
    <div class="si-image">
    <a href="<?php the_permalink() ?>">
    <?php if ( has_post_thumbnail() )
    the_post_thumbnail('si-images');?>
    </a>
    </div>

    <div class="si-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>

    <div class="si-date">&nbsp;Date:&nbsp; 
    <a style="color:#154157;" href="<?php the_permalink() ?>">
    <?php the_time( get_option( 'date_format' ) ) ?>
    </a>
    </div>
    </div>
    <?php endwhile;?>

    <div class="clear"></div>           


    <?php endwhile; endif; ?>

2 Answers 2

0

You don't need to make them separate queries. Query for all 12 posts, when you output them in the first section only output them if $wp_query->current_post < 6, then rewind_posts() and run the loop again, only outputting them in the second section if $wp_query->current_post > 5. If you are using a custom WP_Query, swap $wp_query with whatever your custom query object is named. Using the method of checking which post you are currently outputting, you could probably even just do it in a single loop.

EDIT - example. note that current_post starts at zero, so it's equal to 5 on the sixth post:

$the_query = new WP_Query( array(
    'posts_per_page' => 15
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post();

    if( $the_query->current_post < 6 ) :

        // This is one of the first 6 posts
        // Output the thumbnail, title, etc..

    endif;

    if( $the_query->current_post == 5 ) :

        // The sixth post has been output,
        // output the header/opening container
        // for all the other posts

    endif;

    if( $the_query->current_post > 5 ) :

        // This is post 7 and greater
        // Output just the title, etc..

    endif;

endwhile;

Example 2 - using rewind_posts and running the same loop twice:

$the_query = new WP_Query( array(
    'posts_per_page' => 15
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post();

    if( $the_query->current_post < 6 ) :

        // This is one of the first 6 posts
        // Output the thumbnail, title, etc..

    endif;

endwhile;

// rewind the loop to run it again
$the_query->rewind_posts();

while ( $the_query->have_posts() ) :
    $the_query->the_post();

    if( $the_query->current_post > 5 ) :

        // This is post 7 and greater
        // Output just the title, etc..

    endif;

endwhile;
3
  • I do kind of get what you mean, I have edited my question to show my php, Wordpress PHP is still a learning curve :( Commented Sep 18, 2012 at 12:11
  • well first, you've got a loop within a loop. you can remove the outer loop if you're not actually outputting the page content to which that template is assigned. see edit above.
    – Milo
    Commented Sep 18, 2012 at 16:26
  • This has ended up working perfectly! Thank you very much! Is there a way to display categories also? Commented Sep 23, 2012 at 22:59
0

I'd run two seperate queries, the first with 'showposts' => 6 and the second one with the offset parameter.

http://codex.wordpress.org/Template_Tags/get_posts#Posts_list_with_offset

simply add 'offset'=> 6 to the your query for the 'More Articles'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.