Wordpress - 在帖子和页面上输出自动辅助导航

时间:2012-01-11 15:30:39

标签: php wordpress navigation

我目前有一个脚本可以根据网站菜单输出辅助导航,但是我无法想到将帖子链接到页面以便帖子仍然可以显示辅助导航,这是否可能?这是我用来输出辅助导航的当前代码:

<?php
$secondAncestor = count($post->ancestors) -1; //figure out what level of navigation we are on, subtract one because we don't want to consider the top-level
if($post->post_parent!=0) //if the page is not a top-level category
{
echo '<nav><h2 class="widgettitle">In this section:</h2><ul class="secondary-nav"><li class="sidebarlist">';
//the following lists children of second level ancestor of the current page.
wp_list_pages("title_li=&child_of=".$post->ancestors[$secondAncestor]."&    sort_column=menu_order&echo=1");
echo '</li>';
}
else //if the page is a top-level category
{
//listing only the child pages of the current section
$children= wp_list_pages("title_li=&child_of=".$post->ID."&  sort_column=menu_order&echo=0");
if($children) //this will stop it from displaying a section heading if there are   no elements in the section (for example on the home page)
{
echo '<nav><h2 class="widgettitle">In this section:</h2><ul class="secondary-nav"><li>';
echo $children;

echo '</li>';

}
}
echo '</ul></nav>';
?>

2 个答案:

答案 0 :(得分:1)

页面是历史性的,所以他们可以有父母和孩子。 帖子是平的,并且按类别和标签相关。

为了将帖子与网页相关联,我认为您需要在帖子中使用自定义字段。您可以将其称为父页面ID,然后在侧边栏代码中添加一个自定义查询,以检查其父页面ID =当前页面ID的帖子。

答案 1 :(得分:0)

结帐Flexi Pages Widget。这将为您的侧边栏添加一个高度可配置的子页面菜单,您可以将其嵌入帖子和页。