【发布时间】: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 ?> »</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