最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览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.

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();

发布评论

评论列表(0)

  1. 暂无评论