Suppose there is a page, Games and there are children of this page WarCraft, Half Life, Counter Strike. How to get me all the id pages of children? The fact is that you need to create a lot of parents and children. And that all the time not to look somewhere where id, I want them to just somewhere output, or receive, indicating "Parent". How to do this?

    2 answers 2

    Use the get_page_children () function

    <?php // ΠŸΠΎΠ»ΡƒΡ‡ΠΈΠΌ страницы, ΠΏΠΎ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌ Π±ΡƒΠ΄Π΅Ρ‚ ΠΈΠ΄Ρ‚ΠΈ поиск $my_wp_query = new WP_Query(); $all_wp_pages = $my_wp_query->query(array('post_type' => 'page')); // На ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ страницы с id = 2 $page_id = 2; // Π€ΠΈΠ»ΡŒΡ‚Ρ€ΡƒΠ΅ΠΌ страницы ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½ΠΎΠ³ΠΎ Ρ€Π°Π½Π΅Π΅ массива $all_wp_pages ΠΈ Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ Π² Π½ΠΈΡ… всСх ΠΏΠΎΡ‚ΠΎΠΌΠΊΠΎΠ² страницы с id = $page_id $page_childrens = get_page_children($page_id, $all_wp_pages); //Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ id ΠΈ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΏΠΎΡ‚ΠΎΠΌΠΊΠ° foreach ($page_childrens as $page_children) { echo 'page_id: ' . $page_children->ID . '; page_title: ' . $page_children->post_title . '<br>'; } ?> 
    • Thanks for your code, it does not work, I substituted the ID of the pages, the parents, the children were not output. - KernelByte
    • it's strange because everything worked fine for me - alenkins
    • I do not have, inserted in single.php where the post is displayed, and nothing. Inserted different pages of pages, too. Checked, array "$ page_childrens" and it is empty! maybe somewhere there is an error in getting the children page. - KernelByte

    But thanks, in the meantime, I found the answer on another source, and I didn’t redid the code a lot, let's say I brought it to a normal view, and output the results.

     <?php function get_all_pages($page_title) { $page = get_page_by_title($page_title); if (empty($page)) return array(); $children = new WP_Query( array( 'post_type' => 'page', 'post_parent' => $page->ID, 'fields' => 'ids', 'posts_per_page' => -1 //ПокаТСм всСх Π΄Π΅Ρ‚Π΅ΠΉ ! Π₯ΠΎΡ€ΠΎΡˆΠΈΡ… ΠΈ ΠΏΠ»ΠΎΡ…ΠΈΡ… :) ) ); array_unshift($children->posts,"{$page->ID}"); return $children->posts; } //Имя страницы родитСля, Π°ΠΉΠ΄ΠΈ Π΄Π΅Ρ‚Π΅ΠΉ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Ρ…ΠΎΡ‚ΠΈΠΌ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ. $allpages = get_all_pages('Имя страницы родитСля'); //БдСсь ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ имя страницы (РодитСля) foreach($allpages as $page) { //Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Π°ΠΉΠ΄ΠΈ ΠΈ имя страницы. echo "<br>".$page."->".get_the_title($page); } ?> 

    Maybe someone will be useful. Thanks for the help.