Shortcode – Afficher page enfants de la page parent actif WordPress

Pour le fichier function.php :

function afficher_pages_enfants($atts) {
$atts = shortcode_atts(
array(
'parent_id' => get_the_ID(),
), $atts, 'pages_enfants'
);

$args = array(
    'post_type' => 'page',
    'post_status' => 'publish',
    'post_parent' => intval($atts['parent_id']),
    'orderby' => 'menu_order',
    'order' => 'ASC',
);

$query = new WP_Query($args);
$output = '';

if ($query->have_posts()) {
    $output .= '<ul>';
    while ($query->have_posts()) {
        $query->the_post();
        $output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
    }
    $output .= '</ul>';
    wp_reset_postdata();
} else {
    $output .= 'Aucune page enfant trouvée.';
}

return $output;
}
add_shortcode('pages_enfants', 'afficher_pages_enfants');

Remplacez ID_DE_LA_PAGE par l’ID de la page dont vous souhaitez afficher les enfants :

[pages_enfants parent_id= »ID_DE_LA_PAGE »]

Si vous souhaitez afficher les enfants de la page courante, utilisez simplement :

[pages_enfants]

Hide Preloader