每页上显示的子页列表

时间:2014-07-31 作者:AndrettiMilas

此代码显示父页面及其子页面列表,但不显示祖父母页面。(这是左侧边栏中的第二个红色菜单here).

这有一个问题。此代码输出all pages on the website 如果用户是on a post or archives page (see here).

如何防止这种情况发生?这段代码又大又笨重,有更简单的解决方案吗?谢谢你的帮助。

<?php //parent variables
    $parent = get_post($post->post_parent);
    $parent_title = get_the_title($parent);
    $grandparent = $parent->post_parent;
    $grandparent_title = get_the_title($grandparent);
    $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );?>

<?php // is the homepage the granparent? = third level page
if ($grandparent == is_page(\'0\')) {
    $children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); // list the parent page
    $children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable
    if ($children) { ?>
    <ul class="submenu">
      <?php echo $children; /*print list of pages*/ ?>
    </ul>
    <?php }?>

<?php // is the homepage the parent? = second level page
    } elseif ($post->post_parent ==is_page(\'0\')) {
      $children = wp_list_pages(\'title_li=&child_of=\'.$post->ID.\'&echo=0\');
  if ($children) { ?>

    <ul>
      <li class="current_page_item"><?php echo get_the_title(); ?></li>
      <?php echo $children; ?>
    </ul>
    <?php
    } else {// your else stuff

    }
?>

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

如果您只想将其用于页面,这对于您描述的内容是有意义的,那么只需将整个代码包在if ( is_page() ) { ... }.

如果你要把它变成一个函数,你可以早点退出。

function wpdev_156446_list_parent_page_tree() {

    if ( ! is_page() ) {
        return;
    }

    // Your above code here
}

// EDIT

function wpdev_156446_list_parent_page_tree() {

    if ( ! is_page() ) {
        return;
    }

    $parent = get_post($post->post_parent);
    $parent_title = get_the_title($parent);
    $grandparent = $parent->post_parent;
    $grandparent_title = get_the_title($grandparent);
    $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );

    // is the homepage the granparent? = third level page
    if ($grandparent == is_page(\'0\')) {
        $children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); // list the parent page
        $children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable
        if ($children) {
            ?>
            <ul class="submenu">
              <?php echo $children; /*print list of pages*/ ?>
            </ul>
        <?php
        }

    // is the homepage the parent? = second level page
    } elseif ($post->post_parent ==is_page(\'0\')) {
        $children = wp_list_pages(\'title_li=&child_of=\'.$post->ID.\'&echo=0\');
        if ($children) {
            ?>
            <ul>
              <li class="current_page_item"><?php echo get_the_title(); ?></li>
              <?php echo $children; ?>
            </ul>
        <?php
        } else {// your else stuff

        }
    }
}

结束

相关推荐

Order Admin sub-menu items?

我正在使用向CPT管理菜单添加项目add_submenu_page 这很好,但它们被添加到CPT选项后的子菜单底部。我希望能够将它们放在最上面,但我想这个问题也可以应用于订购所有基于管理员的子菜单项。我所尝试的(不起作用,我尝试了几种变体),function custom_menu_order($menu_ord) { if (!$menu_ord) return true; return array( \'edi