1

我有以下页面结构:

  • 主页(id 5)
    • 子页面 1
      • 儿童页面
      • 儿童页面
    • 子页面 2
      • 儿童页面
      • 儿童页面

我使用此代码显示我的侧边栏菜单:

 <?php wp_list_pages('child_of=5&title_li=&link_before=<span>&link_after=</span>'); ?>

此代码仅显示一个列表。我需要显示

  • 在主页上时,仅显示子页面
  • 在子页面上时,显示我当前所在子页面的子页面和子页面
  • 在子页面时,显示我当前所在的父子页面的显示子页面和子页面

知道怎么做吗?感谢帮助

4

3 回答 3

1

使用 Depth 处理您希望它显示的内容

<?php wp_list_pages('child_of=5&depth=1&title_li=&link_before=<span>&link_after=</span>'); ?>

参考:http ://codex.wordpress.org/Function_Reference/wp_list_pages

默认值为 0(显示所有页面,包括所有子页面)。

    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
于 2013-04-24T00:22:49.160 回答
0

您可以尝试使用这个Hierarchical Pages Widget。我不确定它是否完全支持你想要的,但我在我的一个项目中使用了它并且效果很好。

这个小部件将:

制作折叠分层页面/类别/分类列表:顶级;当前的祖先、孩子和/或兄弟姐妹

于 2013-04-24T08:18:56.643 回答
0

我找到了一个工作代码,对我来说就像一个魅力:

/**
 * Create HTML list of pages.
 *
 * @package Razorback
 * @subpackage Walker
 * @author Michael Fields <michael@mfields.org>
 * @copyright Copyright (c) 2010, Michael Fields
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 *
 * @uses Walker_Page
 *
 * @since 2010-05-28
 * @alter 2010-10-09
 */
class Razorback_Walker_Page_Selective_Children extends Walker_Page {
    /**
     * Walk the Page Tree.
     *
     * @global stdClass WordPress post object.
     * @uses Walker_Page::$db_fields
     * @uses Walker_Page::display_element()
     *
     * @since 2010-05-28
     * @alter 2010-10-09
     */
    function walk( $elements, $max_depth ) {
        global $post;
        $args = array_slice( func_get_args(), 2 );
        $output = '';

        /* invalid parameter */
        if ( $max_depth < -1 ) {
            return $output;
        }

        /* Nothing to walk */
        if ( empty( $elements ) ) {
            return $output;
        }

        /* Set up variables. */
        $top_level_elements = array();
        $children_elements  = array();
        $parent_field = $this->db_fields['parent'];
        $child_of = ( isset( $args[0]['child_of'] ) ) ? (int) $args[0]['child_of'] : 0;

        /* Loop elements */
        foreach ( (array) $elements as $e ) {
            $parent_id = $e->$parent_field;
            if ( isset( $parent_id ) ) {
                /* Top level pages. */
                if( $child_of === $parent_id ) {
                    $top_level_elements[] = $e;
                }
                /* Only display children of the current hierarchy. */
                else if (
                    ( isset( $post->ID ) && $parent_id == $post->ID ) ||
                    ( isset( $post->post_parent ) && $parent_id == $post->post_parent ) ||
                    ( isset( $post->ancestors ) && in_array( $parent_id, (array) $post->ancestors ) )
                ) {
                    $children_elements[ $e->$parent_field ][] = $e;
                }
            }
        }

        /* Define output. */
        foreach ( $top_level_elements as $e ) {
            $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
        }
        return $output;
    }
}
于 2013-04-24T21:31:56.290 回答