【问题标题】:Wordpress - output automated secondary navigation on posts as well as pagesWordpress - 在帖子和页面上输出自动辅助导航
【发布时间】:2012-01-11 15:30:39
【问题描述】:

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

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

【问题讨论】:

  • 您的最终结果尚不清楚。提供有关辅助导航何时/如何出现的更多说明。
  • 它当前输出您所在的页面以及与当前页面有关系的任何孩子,问题是我不确定如何在页面和帖子之间创建关系

标签: php wordpress navigation


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 谢谢,但它似乎不允许您在菜单中输出帖子,只能输出我已经拥有的代码的页面
    猜你喜欢
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 2011-03-14
    • 2013-05-26
    • 2013-10-21
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    相关资源
    最近更新 更多