最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Get Child of Child Pages in custom Menu

programmeradmin0浏览0评论

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',
    ]
);
?>
发布评论

评论列表(0)

  1. 暂无评论