wp query - How to display particular set of wordpress post on a webpage?

I have a wordpressphp code as shown below which display list of posts on a webpage.Php code:$special_reports = new WP_

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.

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.

Share Improve this question edited Aug 7, 2019 at 21:00 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Aug 7, 2019 at 20:32 johnjohn 571 gold badge1 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745258894a4619118.html

相关推荐

  • wp query - How to display particular set of wordpress post on a webpage?

    I have a wordpressphp code as shown below which display list of posts on a webpage.Php code:$special_reports = new WP_

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信