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
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.
|