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


Wordpress - output automated secondary navigation on posts as well as pages

我目前有一个基于网站菜单输出辅助导航的脚本,但我无论如何都想不出将帖子链接到页面,这样帖子仍然可以显示辅助导航,这可能吗?这是我用来输出辅助导航的当前代码:

<?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>';
?>

页面是分层的,因此它们可以有父母和孩子。帖子是扁平的,并通过类别和标签进行关联。

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

查看Flexi页面小工具。这将为你的侧边栏添加一个高度可配置的子页面菜单,你可以将其嵌入帖子&页面。