, ,

wordpress で 子ページを取得して表示する

get_children で 子ページを取得できます

<?php
global $post;
$args = array(
    'post_parent' => $post->ID,
    'post_status' => 'publish',
    'post_type' => $post->post_type,
);
$children = get_children($args);

if (count($children) > 0) :
    ?>
    <ul>
        <?php
        foreach ($children as $child) :
            ?>
            <li>
                <a href="<?php echo esc_url(get_permalink($child->ID)); ?>">
                    <?php echo esc_attr($child->post_title); ?>
                </a>
            </li>
            <?php
        endforeach;
        ?>
    </ul>
    <?php

endif;

こんな感じ

Categorised in: , ,