创建高度定制的子菜单(可能使用wp_list_ages)

时间:2016-08-07 作者:Heather

我想创建一个子菜单,列出父页的子级,最多两级。我有一些代码,但我需要高度定制它。我不明白the codex 足以弄清楚如何做到这一点。这就是我需要的代码。

例如,菜单层次结构为:

当在“家长”页面上时,代码如下所示:

<ul>
   <li><a href="/child-1/">Child 1</a></li>
   <li>Child 2
      <ul class="child">
        <li><a href="/child-2/">Overview</a></li>
        <li><a hres="/child-2-1/">Child 2.1</a></li>
      </ul>
   </li>
</ul>
请注意li “与子项”没有链接,它的第一个子项有其父项与标题“概述”的链接。

这是我目前拥有的代码:

<?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 } ?>
此代码输出:

<ul>
   <li><a href="/child-1/">Child 1</a></li>
   <li><a class="page_item_has_children" href="/child-2/">Child 2</a>
      <ul class="child">
        <li><a hres="/child-2-1/">Child 2.1</a></li>
      </ul>
   </li>
</ul>

EDIT

我一直在做进一步的搜索,发现了一些可能有用的代码,唯一的问题是,它输出所有页面(而不仅仅是子页面)。结果都比我需要的高出一个级别(请参见概述的位置)。

此代码:

<?php 
    $Pages      = wp_list_pages(\'title_li=&echo=0&depth=1\');
    $InnerPages = wp_list_pages(\'child_of=\'.($post->post_parent != false ? $post->post_parent : $post->ID).\'&title_li=&echo=0\');
    $Title      = ($post->post_parent != false) ? trim(get_the_title($post->post_parent)) : trim(wp_title(\'\', false));
    $Link      = ($post->post_parent != false) ? trim(get_the_title($post->post_parent)) : trim(get_permalink(\'\', false));
    if($Title != \'\')
        $Pages      = str_replace($Title.\'</a></li>\',
                                  $Title.\'</a>\'.
                                 \'<ul id="test"><li><a href="\'.$Link.\'">Overview</a></li>\'.$InnerPages.\'</ul></li>\',
                                $Pages);
    echo $Pages;
    unset($Pages, $InnerPages);

    ?>
输出:

<ul>
<li><a href="/home/">Home</a></li>
<li><a href="/parent/">Parent</a>
    <ul id="test">
       <li><a href="/parent/">Overview</a></li>
       <li><a href="/child-1/">Child 1</a></li>
       <li><a href="/child-2/">Child 2</a>
           <ul class="child">
             <li><a hres="/child-2-1/">Child 2.1</a></li>
           </ul>
       </li>
     </ul>
  </li>
 </ul>

1 个回复
最合适的回答,由SO网友:Heather 整理而成

经过几天的研究,我能够循环查看wp_list_pages 按我需要的方式自定义菜单。

<?php
if(count(get_post_ancestors($post->ID)) == 1 ) {
    echo \'<ul>\';

    $args = array(
        \'post_type\' => \'page\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => -1,
        \'post_parent\' => $post->post_parent,
    );
    $query = new WP_Query($args);
    while ($query->have_posts()) {
        $query->the_post();

        $child = get_pages(\'child_of=\'.$post->ID);

            if( count( $child ) != 0 ) : ?>
                <li class="has-children"><span><?php the_title(); ?></span>
                <?php $children = wp_list_pages( \'title_li=&child_of=\'.$post->ID.\'&echo=0\' );
                if ( $children) : ?>
                    <ul class="children">
                        <li <?php if(is_page($post->ID )) {?> class="current_page_item" <?php }?> ><a href="<?php the_permalink(); ?>">Overview</a></li>
                        <?php echo $children; ?>
                    </ul>
                <?php endif; ?>
                </li>

            <?php else : ?>

                <li <?php if(is_page($post->ID )) {?> class="current_page_item" <?php }?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

            <?php endif; 
        }
    echo \'</ul>\';
}
elseif (count(get_post_ancestors($post->ID)) == 2 ) {
    echo \'<ul>\';
    $args = array(
        \'post_type\' => \'page\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => -1,
        \'post_parent\' => get_post( $post->post_parent )->post_parent,
        \'depth\' => 1,
    );
    $query = new WP_Query($args);
    while ($query->have_posts()) {
        $query->the_post();

        $child = get_pages(\'child_of=\'.$post->ID);

        if( count( $child ) != 0 ) : ?>
            <li class="has-children"><span><?php the_title(); ?></span>
            <?php $children = wp_list_pages( \'title_li=&child_of=\'.$post->ID.\'&echo=0\' );
            if ( $children) : ?>
                <ul class="children">
                    <li <?php if(is_page($post->ID )) {?> class="current_page_item" <?php }?>><a href="<?php the_permalink(); ?>">Overview</a></li>
                    <?php echo $children; ?>
                </ul>
            <?php endif; ?>
            </li>

            <?php else : ?>

            <li <?php if(is_page($post->ID )) {?> class="current_page_item" <?php }?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

            <?php endif; 
        }
    echo \'</ul>\';
}
wp_reset_postdata();
?>