php - Pulling Individual Posts from WP_Query

admin2025-06-05  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!

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!

Share Improve this question asked Dec 30, 2018 at 21:30 FarhadDFarhadD 1034 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749057369a315991.html

最新回复(0)