0

I have a wordpress/php code as shown below which display list of posts on a webpage.

Php code:

$special_reports = new \WP_Query([
    'post_type' => 'cpac-special-report',
    'orderby' => 'menu_order',
    'order' => 'ASC',
    'fields' => 'ids',
    'posts_per_page' => $data->{"toggle_multi_status"} == 2 ? 3 : 4
]);

On debug <?php echo json_encode($special_reports); ?> it displays the following:

{
    "query": {
        "post_type": "cpac-special-report",
        "orderby": "menu_order",
        "order": "ASC",
        "fields": "ids"
    },
    "posts": [149443, 149551, 149488],   /* Posts id here is displaying on the basis of weight order ASC */
    "max_num_pages": 54
}

What I want now in the following php, I want post id 149443 to show up at Line A and post ids 149551 and 149488 at Line B.

<?php if ($data->{"toggle_multi_status"} == 2) { ?>

<?php if ($special_reports->have_posts()) : while ($special_reports->have_posts()) : $special_reports->the_post(); ?>
          // Here I want to display post id 149443 (lets call as tile A)                         /* Line A */     
<?php endwhile; endif;

<div class ="test">
    <p>Some Content</p> // Here I am displaying tile B          
</div>

<?php if ($special_reports->have_posts()) : while ($special_reports->have_posts()) : $special_reports->the_post(); ?>
         // Here I want to display post ids 149551 and  149488  (lets call as tiles C and D)    /* Line B */    
<?php endwhile; endif;

<?php                   
}                   
?>  

The graphical representation of the above php code should be:

Tile A   Tile B       Tile C   Tile D
149443   Some Content 149551   149488

Problem Statement:

I am wondering what changes I should make in the code above so that 1st post id 149443 displays at Line A and 2nd/3rd post ids (149551 and 149488) display at Line B.

0

To display post id 149443, inside the first while() loop just use if ( get_the_ID() == 149433 ) and for the others use if ( get_the_ID() != 149433 ). The trick is to rewind the loop between the 2 uses so that you can use it again, so after the first loop use $special_reports->rewind_posts();

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.