0

I have tried many different versions of this and nothing seems to be working for me. I want to display single posts of a custom post type (portfolio) each in its own div. I don't want any duplicate posts on the page. I need 13 different divs, each containing info about a different post. Link to the page in question: http://ww3.gbs-inc.com/featured-projects/

<div class="port1">
    <?php $my_query = new WP_Query( 'post_type=portfolio&showposts=1' );
    while ( $my_query->have_posts() ) : $my_query->the_post();
    $do_not_duplicate = $post->ID; ?>
        <?php the_title(); ?>
    <?php endwhile; ?>
</div>

<div class="port2">
    <?php while ( $my_query->have_posts() ) : $my_query->the_post();
    if ( $post->ID == $do_not_duplicate ) continue; ?>
        <?php the_title(); ?>
    <?php endwhile; ?>
</div>

<div class="port3">
    <?php while ( $my_query->have_posts() ) : $my_query->the_post();
    if ( $post->ID == $do_not_duplicate ) continue; ?>
        <?php the_title(); ?>
    <?php endwhile; ?>
</div>
1

Is there a reason to execute the query in each individual div instead of looping the div within the query?

<?php
$my_query = new WP_Query( 'post_type=portfolio&showposts=13' ); 
while ( $my_query->have_posts() ) :
$my_query->the_post();
?>
  <div class="port1">
    <?php the_title(); ?>
  </div>
<?php endwhile; ?>

If you want to number the divs sequentially, you can use a counter:

<?php
$i=0;
$my_query = new WP_Query( 'post_type=portfolio&showposts=13' ); 
while ( $my_query->have_posts() ) :
$my_query->the_post();
?>
  <div class="port<?php echo $i++; ?>">
    <?php the_title(); ?>
  </div>
<?php endwhile; ?>
  • your second option, using a counter, is a very cool and simple way of accomplishing this. Thanks for that! I have to style the page exactly as the client wants it so I may have to revisit this if I can't get the divs to play nicely with each other. – Patrick McGilvray Mar 25 '15 at 14:48
  • @PatrickMcGilvray - glad to hear. feel free to mark it as answered. – Charles Mar 25 '15 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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