0

I'm trying to run a WP_Query and then put each of the 3 posts pulled from that query into a differently formatted div in my template. However, when the code below runs, I end up getting the first post only showing in all 3 div blocks.

I suspect that $cases1->the_post[0] isn't working the way I'm hoping it would, but I can't quite figure out how to do this. Could someone with a better understanding of the WP codex point me in the right direction, please?

<?php $postid = array(get_the_ID()); ?>
<?php if ( get_post_type() == 'case_study' ) {
   $argsCS = array (
   'post_type' => 'case_study',
   'posts_per_page' => 3,
   'orderby' => 'desc',
   'post__not_in' => $postid
   );
   $cases1 = new WP_Query($argsCS); if ($cases1->have_posts()) : 
?>
    <div id="side-1" class="column column-6-12">
        <div class="nw">
                <?php $cases1->the_post[0]; ?>
                <?php include(TEMPLATEPATH."/load-posts-sidebar.php"); ?>
         </div>
    </div><!-- columns-6-12 -->
    <div id="side-2" class="column column-6-12">
        <div class="wp">
            <?php $cases1->the_post[1]; ?>
            <?php include(TEMPLATEPATH."/load-posts-sidebar.php"); ?>
        </div>
        <div class="cs botMargin10">
            <?php $cases1->the_post[2]; ?>
            <?php include(TEMPLATEPATH."/load-posts-sidebar.php"); ?>
        </div>
    </div>

Also, the $postid = array(get_the_ID()); seems to be setting ID of the post that gets shown in each of the 3 divs rather than preventing that post from being shown!

1

Close... But you can’t make the syntax up yourself ;)

<?php $postid = array(get_the_ID()); ?>
<?php if ( get_post_type() == 'case_study' ) {
   $argsCS = array (
   'post_type' => 'case_study',
   'posts_per_page' => 3,
   'orderby' => 'desc',
   'post__not_in' => $postid
   );
   $cases1 = new WP_Query($argsCS); if ($cases1->have_posts()) : 
?>
    <div id="side-1" class="column column-6-12">
        <?php if ( $cases1->have_posts() : $cases1->the_post(); ?>
        <div class="nw">
                <?php include(TEMPLATEPATH."/load-posts-sidebar.php"); ?>
        </div>
        <?php endif; ?>
    </div><!-- columns-6-12 -->
    <div id="side-2" class="column column-6-12">
        <?php if ( $cases1->have_posts() : $cases1->the_post(); ?>
        <div class="wp">
            <?php include(TEMPLATEPATH."/load-posts-sidebar.php"); ?>
        </div>
        <?php endif; ?>
        <?php if ( $cases1->have_posts() : $cases1->the_post(); ?>
        <div class="cs botMargin10">
            <?php include(TEMPLATEPATH."/load-posts-sidebar.php"); ?>
        </div>
         <?php endif; ?>
    </div>

Also you shouldn’t use include in your templates - use get_template_part() instead.

  • Hahahaha "But you can’t make the syntax up yourself ;)" That is exactly what I did... guilty as charged :-D – FarhadD Dec 30 '18 at 21:49
  • I thought so ;) but you were really close :) – Krzysiek Dróżdż Dec 30 '18 at 21:50
  • It works! Thank you very much!! (Though I must admit the logic behind including the if ($cases1->have_posts()) outside the divs and then repeating it inside each div is confusing me – FarhadD Dec 30 '18 at 21:58
  • 1
    You should check if there are any posts left before every call of the_post (it loads next posts from the loop, so it may cause problems if there are no more posts ;)) – Krzysiek Dróżdż Dec 30 '18 at 22:00

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.