最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Pulling Individual Posts from WP_Query

matteradmin8PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far