Display an array of specific pages with WP_Query in Wordpress

What I'm trying to achieve shows only certain pages in a loop. 3 pages, no more, no less.

I have tried many things, but I just cannot complete it.

<?php
    $special_tabs = new WP_Query(array ('post_type'=>'page','post__in'=>array(100,102,104)))
?>

What this means, from what I understand, is that it shows an array of pages, and then includes those IDs.

<?php
    $special_tabs = new WP_Query('page_id=100');
?>

And this shows only one page, it does not support showing an array of different identifiers.

I am sure that I am not the first person to have such specific needs, and I am sure that there is a relatively simple way to achieve what I am trying to achieve, but I simply cannot find a solution or find it. Can someone help me with this?

Thank you very much in advance!

+3
2

, - get_pages() wp_query()?

, .

<ul>
<?php $special_tabs = get_pages ('sort_column=post_title&echo=0&include=2,3,4&title_li='); ?>
<?php 
foreach ($special_tabs as $tab) {
  $title = $tab->post_title;
  echo "<li>".$title."</li>";
}
?>
</ul>

print_r $special_tab,

</php
echo"<pre>";
echo print_r($special_tabs);
echo"</pre>";
?>


    Array
(
    [0] => stdClass Object
        (
            [ID] => 2
            [post_author] => 1
            [post_date] => 2010-03-24 15:26:18
            [post_date_gmt] => 2010-03-24 15:26:18
            [post_content] => This is an example of a WordPress page.
            [post_title] => About
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => open
            [ping_status] => open
            [post_password] => 
            [post_name] => about
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2010-03-24 15:26:18
            [post_modified_gmt] => 2010-03-24 15:26:18
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://example.com/about/
            [menu_order] => 0
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [1] => stdClass Object
        (
            [ID] => 3
            [post_author] => 1
            [post_date] => 2010-03-27 10:48:29
            [post_date_gmt] => 2010-03-27 10:48:29
            [post_content] => 
            [post_title] => testpage1
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => open
            [ping_status] => open
            [post_password] => 
            [post_name] => testpage1
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2010-03-27 10:48:29
            [post_modified_gmt] => 2010-03-27 10:48:29
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://example.com/testpage1/
            [menu_order] => 0
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [2] => stdClass Object
        (
            [ID] => 4
            [post_author] => 1
            [post_date] => 2010-03-27 10:56:26
            [post_date_gmt] => 2010-03-27 10:56:26
            [post_content] => 
            [post_title] => testpage2
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => open
            [ping_status] => open
            [post_password] => 
            [post_name] => testpage2
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2010-03-27 10:56:26
            [post_modified_gmt] => 2010-03-27 10:56:26
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://example.com/testpage2/
            [menu_order] => 0
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

)

, , ..:) , YOUR PAGE ID get_pages() include, 2,3,4, ...

ref: http://codex.wordpress.org/Function_Reference/get_pages

+1

2+ :

, WP_Query foreach:

$certain_pages = array(100,102,104);
foreach( $certain_pages as $a_page ) {
    $special_tabs = new WP_Query('page_id='.$a_page);
    /* (sample) loop goes here */
    if ($special_tabs->have_posts()) : while ($special_tabs->have_posts()) : $special_tabs->the_post();
        the_content();
    endwhile; endif;
    /* loop ends */
}

, , .

0

Source: https://habr.com/ru/post/1740284/


All Articles