【问题标题】:WordPress sidebar navigation based off of top-level parent page基于顶级父页面的 WordPress 侧边栏导航
【发布时间】:2014-06-06 13:32:15
【问题描述】:

我正在尝试创建一个复杂的侧边栏导航系统,该系统根据您正在查看的顶级页面保持不变。例如,假设我有这个导航:

  • 首页
  • 关于
    • 我们公司
      • 位置
      • 小时
    • 我们的员工
      • 吉姆
      • 戴夫
      • 莎拉
      • 凯莉
  • 投资组合
    • 标志
    • 网站
  • 联系方式

现在,如果用户位于“关于”部分的任何位置,我希望显示侧边栏:

  • 关于
    • 我们公司
      • 位置
      • 小时
    • 我们的员工
      • 吉姆
      • 戴夫
      • 莎拉
      • 凯莉

如果它们位于“关于”主页面、“我们的公司”页面或“位置”页面中,则适用。无论深度如何,我都需要整个导航可见。

如果用户在没有子项的页面中,例如联系人,则侧边栏需要显示:

  • 首页
  • 关于
  • 投资组合
  • 联系方式

更重要的是,排序需要基于 WordPress 菜单(一个主菜单,每个侧边栏不能是自己的;这对用户来说太复杂了)。我不知道这是否会使事情复杂化。

过去,我设法获得了一些东西来显示子页面和同级页面,但它不显示父页面,也不按用户定义的顺序显示。

<ul>
    <?
    global $wp_query;
    if( empty($wp_query->post->post_parent) ) {
        $parent = $wp_query->post->ID;
    }
    else {
        $parent = $wp_query->post->post_parent;
    }
    wp_list_pages ("&title_li=&child_of=$parent");
    ?>
</ul>

如果可以修改它以按照我想要的方式工作,那就太好了。我将尝试自己解决这个问题,并会在我取得进展时发布更新。

更新 1:我在确定绝对父母是什么方面取得了一些进展。我认为我在正确的轨道上。

<?
if ($post->post_parent) {
    $ancestors = get_post_ancestors($post->ID);
    $root = count($ancestors)-1;
    $parent = $ancestors[$root];
} else {
    $parent = $post->ID;
}
echo "Current Page: " . $post->ID . "<br />";
echo "Top Level Parent: " . $parent;
?>

更新 2:我现在可以确定查询菜单的正确页面 ID,所以我想我快接近了。

<?
$children = get_pages("child_of=".$post->ID);
if ($post->post_parent) {
    $ancestors = get_post_ancestors($post->ID);
    $root = count($ancestors)-1;
    $parent = $ancestors[$root];
} elseif (count($children) != 0) {
    $parent = $post->ID;
} else {
    $parent = 0;
}
echo "Current Page: " . $post->ID . "<br />";
echo "Top Level Parent: " . $parent;
?>

更新 3:我快到了!唯一的问题是这使用了来自编辑器的页面顺序,而不是来自菜单。是否可以编辑它以使用菜单代替?

<aside>
    <?
    $children = get_pages("child_of=".$post->ID);
    if ($post->post_parent) {
        $ancestors = get_post_ancestors($post->ID);
        $root = count($ancestors)-1;
        $parent = $ancestors[$root];
        $sidebarDepth = 0;
        $postParentID = get_post($parent);
        $title = $postParentID->post_title;
    } elseif (count($children) != 0) {
        $parent = $post->ID;
        $sidebarDepth = 0;
        $postParentID = get_post($parent);
        $title = $postParentID->post_title;
    } else {
        $parent = 0;
        $sidebarDepth = 1;
        $frontPageID = get_option("page_on_front");
        $exclude = $frontPageID;
        $postParentID = get_post($frontPageID);
        $title = $postParentID->post_title;
    }
    ?>
    <header>
        <h6><a href="#"><? echo $title ?> &raquo;</a></h6>
    </header>
    <section>
        <nav>
            <?
            echo "<ul>";
            wp_list_pages("child_of=" . $parent . "&depth=" . $sidebarDepth . "&exclude=" . $exclude . "&title_li=&");
            echo "</ul>";
            ?>
        </nav>
    </section>
</aside>

【问题讨论】:

    标签: php wordpress menu navigation


    【解决方案1】:

    想通了!我从this answer 修改了代码,将start_in 选项添加到wp_nav_menu,并从那里修改了我的代码。现在这完全符合我的要求。

    functions.php:

    // add start_in argument to navigation
    add_filter("wp_nav_menu_objects",'my_wp_nav_menu_objects_start_in',10,2);
    function my_wp_nav_menu_objects_start_in( $sorted_menu_items, $args ) {
        if (isset($args->start_in) && $args->start_in != 0) {
            $menu_item_parents = array();
            foreach ($sorted_menu_items as $key => $item) {
                if ($item->object_id == (int)$args->start_in) $menu_item_parents[] = $item->ID;
                if (in_array($item->menu_item_parent, $menu_item_parents)) {
                    $menu_item_parents[] = $item->ID;
                } else {
                    unset($sorted_menu_items[$key]);
                }
            }
            return $sorted_menu_items;
        } else {
            return $sorted_menu_items;
        }
    }
    

    page.php(或任何你想要的模板):

        <aside>
        <?
        if ($post->post_parent) {
            $ancestors = get_post_ancestors($post->ID);
            $root = count($ancestors)-1;
            $parent = $ancestors[$root];
        } elseif (count(get_pages("child_of=".$post->ID)) != 0) {
            $parent = $post->ID;
        } else {
            $parent = get_option("page_on_front");
            $sidebarDepth = 1;
            $exclude = $parent;
        }
        if ($post->post_parent || count(get_pages("child_of=".$post->ID)) != 0) {
            $sidebarDepth = 0;
            $start_in = $parent;
            $depth = 0;
        } else {
            $depth = 1;
            $start_in = 0;
        }
        $parentID = get_post($parent);
        $parentTitle = $parentID->post_title;
        $parentURL = get_permalink($parentID);
        ?>
        <header>
            <h6><a href="<? echo $parentURL ?>"><? echo $parentTitle ?> &raquo;</a></h6>
        </header>
        <section>
            <nav>
                <?
                wp_nav_menu(
                    array(
                        "container" => false,
                        "depth" => $depth,
                        "items_wrap" => '<ul>%3$s</ul>',
                        "start_in" => $start_in,
                        "theme_location" => "first"
                    )
                );
                ?>
            </nav>
        </section>
    </aside>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多