如何在网页上显示一组特定的WordPress帖子?

时间:2019-08-07 作者:john

我有一个wordpress/php 显示网页上帖子列表的代码如下所示。

Php代码:

$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
]);
调试时<?php echo json_encode($special_reports); ?> 它显示以下内容:

{
    "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
}
我现在在下面的php中想要什么,我想要post id 149443 出现在Line Apost ids 149551 and 149488Line 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                   
}                   
?>  
上述php代码的图形表示应为:

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

Problem Statement:

我想知道我应该在上面的代码中做些什么更改,以便1st post id 149443 displays at Line A2nd/3rd post ids (149551 and 149488) display at Line B.

1 个回复
SO网友:Peter HvD

要显示post id 149443,请在第一个while() 循环仅使用if ( get_the_ID() == 149433 ) 供其他人使用if ( get_the_ID() != 149433 ). 诀窍是在两次使用之间倒带循环,以便可以再次使用它,所以在第一次使用循环之后$special_reports->rewind_posts();