【问题标题】:How do I create a page list in wordpress that includes parents, children, and siblings, but only in certain cases?如何在 wordpress 中创建包含父母、孩子和兄弟姐妹的页面列表,但仅在某些情况下?
【发布时间】:2012-08-14 01:06:21
【问题描述】:

我正在尝试创建一个侧边栏导航菜单,该菜单会根据站点中的当前位置进行更改。

这是整个导航的样子:

  • 首页
  • 关于我们
    • 1a 级
    • 1b 级
    • 1c 级
  • 程序
    • 1d 级
      • 2a 级
      • 2b 级
    • 1e 级
      • 2c 级
      • 2d 级
    • 1f 级
  • 联系我们

目前我正在使用自定义导航菜单为主页创建菜单,以便他们可以通过管理面板中的菜单链接编辑自定义菜单。他们不希望联系人页面上有侧边栏导航菜单。

在关于我们的页面上,他们希望侧边栏导航看起来像这样:

  • 1a 级
  • 1b 级
  • 1c 级

在“程序”页面上时,他们希望它看起来像这样:

  • 1d 级
  • 1e 级
  • 1f 级

在 1d 级页面上时,他们希望它看起来像这样:

  • 1d 级
    • 2a 级
    • 2b 级
  • 1e 级
  • 1f 级

确保子页面仅显示给当前父页面。

在 2a 级页面上时,他们希望它看起来与在 1d 级页面上一样:

  • 1d 级
    • 2a 级
    • 2b 级
  • 1e 级
  • 1f 级

另外需要注意的是,我需要能够以不同于其他项目的方式设置当前页面列表项的样式,因此需要添加某种当前页面项类每次都列出项目。

到目前为止,我无法在 Level 1d 上仅打印当前父级的子级。 wp_list_pages 允许您设置深度,但不限于仅当前父级。它还打印所有兄弟姐妹的孩子(即,如果在级别 1d 上,它会打印其各自父母下的所有 2 级孩子,而不是仅打印当前活动父母下的 2 级孩子)。

我该怎么做?

    <?php $current_section = "";
        if(is_front_page()):
             $current_section = "home";
             $nav_args = array(
                 'theme_location' => $current_section,
                  'container' => 'nav',
                  'container_class' => 'side-nav'
             );
             wp_nav_menu( $nav_args );
        else:

        endif;?>

这就是我目前所拥有的。我已经删除了 else 块中的所有代码,因为它仍然不起作用。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    尝试以下代码仅显示当前父级及其子菜单

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

    This section of codex explain this

    另外需要注意的是,我需要能够为 current-page list-item 与其余项目不同,所以 需要添加某种当前页面项类 每次都列出项目。

    不需要每次都写一个特殊的类。 WordPress 会自动添加一个类.current-menu-item。您只需要从 CSS 自定义该类。

    希望这能解决您的一些问题.. :)

    【讨论】:

    • 这并不能完全回答我的问题......我的需求非常具体,需要子页面以不同的方式显示第 2 层和第 3 层。这段代码不适应。
    【解决方案2】:

    这是我想出的代码:

        <?php
                $children = wp_list_pages('depth=1&title_li=&child_of='.$post->ID.'&echo=0');
                $current_page = wp_list_pages('title_li=&include='.$post->ID.'&echo=0');
                $grandparent=0;
                if($post->post_parent):
                    $parent = wp_list_pages('title_li=&include='.$post->post_parent.'&echo=0');
                    $siblings = wp_list_pages('depth=1&title_li=&child_of='.$post->post_parent.'&exclude='.$post->ID.'&echo=0');
                    $parent_children = wp_list_pages('depth=1&title_li=&child_of='.$post->post_parent.'&echo=0');
                    $parent_post = get_post($post->post_parent);
                    $grandparent = $parent_post->post_parent;
                    $parent_siblings = wp_list_pages('depth=1&title_li=&child_of='.$grandparent.'&echo=0&exclude='.$post->post_parent);
                endif;
                if(!$post->post_parent && $children):?>
                    <ul class="menu">
                        <?php echo $children;?>
                    </ul>
                <?php elseif($post->post_parent && $children):?>
                    <ul class="menu">
                        <?php echo $current_page;?>
                        <ul class="menu">
                            <?php echo $children;?>
                        </ul>
                        <?php echo $siblings;?>
                    </ul>
                <?php elseif($grandparent && $post->post_parent && !$children):?>
                    <ul class="menu">
                        <?php echo $parent;?>
                        <ul class="menu">
                        <?php echo $parent_children;?>
                        </ul>
                        <?php echo $parent_siblings;?>
                    </ul>
                <?php elseif(!$grandparent && $post->post_parent && !$children):?>
                    <ul class="menu">
                        <?php echo $parent_children;?>
                    </ul>
                <?php endif;?>
    

    这不是有史以来最优雅的解决方案,但至少它符合我的要求。唯一可以改进其实际功能的是保持菜单项的顺序,同时仍然允许子菜单。此代码删除带有子菜单的菜单项并将其移动到顶部。我想不出任何其他方法来做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 2015-03-24
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多