admin管理员组

文章数量:1419666

Got this bit of code which runs which gets the Page ID and displays all the children under it. Which is good.

Part two of this is getting it to pull in the child of children items.

So currently looks like this

Main Menu Item
     Child Item
     Child Item
     Child Item

But I need it to look like this

Main Menu Item
     Child Item
          Child Item
          Child Item
          Child Item

This is the code I am using

<?php
    $args = array(
        'post_type'      => 'page',
        'posts_per_page' => -1,
        'post_parent'    => 92,
    );
    $parent = new WP_Query( $args );
    if ( $parent->have_posts() ) : ?>
        <?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
            <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
        <?php endwhile; ?>

    <?php endif; wp_reset_postdata(); ?>

Got this bit of code which runs which gets the Page ID and displays all the children under it. Which is good.

Part two of this is getting it to pull in the child of children items.

So currently looks like this

Main Menu Item
     Child Item
     Child Item
     Child Item

But I need it to look like this

Main Menu Item
     Child Item
          Child Item
          Child Item
          Child Item

This is the code I am using

<?php
    $args = array(
        'post_type'      => 'page',
        'posts_per_page' => -1,
        'post_parent'    => 92,
    );
    $parent = new WP_Query( $args );
    if ( $parent->have_posts() ) : ?>
        <?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
            <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
        <?php endwhile; ?>

    <?php endif; wp_reset_postdata(); ?>
Share Improve this question asked Jul 17, 2019 at 14:23 JamesJames 34 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can do what you need to with the wp_list_pages() function:

<?php
wp_list_pages(
    [
        'child_of'    => 92,
        'title_li'    => false,
        'sort_column' => 'menu_order',
    ]
);
?>

本文标签: phpGet Child of Child Pages in custom Menu