워드프레스

Get Child and Grandchild Pages in WordPress

janggos 2023. 3. 18. 15:22

https://wpdevdesign.com/get-child-and-grandchild-pages-in-wordpress/

 

This tutorial provides code snippets for pulling all descendants of the current Page including grandchild Pages in WordPress.

Given the above Pages structure, when Page A is being viewed:

Unordered List

<ul>
    <?php
    wp_list_pages( array(
        'title_li'    => '',
        'child_of'    => get_the_ID(),
    ) );
    ?>
</ul>

 

 

 

 

 

IDs Array – Method 1

<?php

$child_pages = get_pages(
  array(
'child_of' => get_the_ID(),
)
);

$child_pages_ids = wp_list_pluck( $child_pages, 'ID' );

echo '<pre>' . print_r( $child_pages_ids, true ) . '</pre>';

?>

 

 

 

 

 

IDs Array – Method 2

<?php

$child_pages_objects = get_page_children( get_the_ID(), get_pages() );

$child_pages_ids = wp_list_pluck( $child_pages_objects, 'ID' );

echo '<pre>' . print_r( $child_pages_ids, true ) . '</pre>';

?>

 

 

 

 

 

 

 

 

https://developer.wordpress.org/reference/functions/get_pages/

 

WordPress Developer Resources | Official WordPress Developer Resources

Official WordPress developer resources including a code reference, handbooks (for APIs, plugin and theme development, block editor), and more.

developer.wordpress.org

https://developer.wordpress.org/reference/functions/get_page_children/

 

WordPress Developer Resources | Official WordPress Developer Resources

Official WordPress developer resources including a code reference, handbooks (for APIs, plugin and theme development, block editor), and more.

developer.wordpress.org