WordPress - listing only 1 tier of children

2019-09-11 19:53发布

问题:

Using the code below, how can I display only the immediate set of children associated with the current page and not the children's children or the other parents on the tier of that page. I assume that the depth parameter is a part of the solution however, I'm unsure as to how to best implement it. Also, does the depth refer to the hierarchy in an absolute or relative fashion. I'd like for it to show only 1 level of depth relative to the page that is selected.

Thank you much for the help and if I can offer any clarification, please let me know.

 <?php
    if($post->post_parent)
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    else
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    if ($children) { ?>
    <ul>
    <?php echo $children; ?>
    </ul>
  <?php } ?>

回答1:

depth is used in this way:

wp_list_pages("depth=1&title_li=&child_of=".$post->ID."&echo=0");

What happens if you use this one?

from documentation:

depth: (integer) This parameter controls how many levels in the hierarchy of pages are to be included in the list generated by wp_list_pages. The default value is 0 (display all pages, including all sub-pages).

0 (default) Displays pages at any depth and arranges them hierarchically in nested lists

-1 Displays pages at any depth and arranges them in a single, flat list

1 Displays top-level Pages only

2, 3 … Displays Pages to the given depth

.



标签: wordpress