我只是找不到一种方法,可以在垂直菜单“wordpress方式”中只显示当前父项到当前页面项。
如果我访问第3.2.2.2页,我希望实现以下动态结构:
第1页第2页Page 3 第4页
第3.1页Page 3.2
- Page 3.2.2<第3.2.2.1页Page 3.2.2.2<第3.2.2.2.1页Page 3.2.2.2\'即使第3.4页或第3.2.3页也有子菜单,也会列出它的父菜单和它自己的子菜单(如果它有子菜单)。
我只是找不到一种方法,可以在垂直菜单“wordpress方式”中只显示当前父项到当前页面项。
如果我访问第3.2.2.2页,我希望实现以下动态结构:
第1页第2页Page 3 第4页
第3.1页Page 3.2
最后,我自己解决了。以下是解决方案:
在里面functions.php:
function show_all_children($parent_id, $post_id, $current_level)
{
$top_parents = array();
$top_parents = get_post_ancestors($post_id);
$top_parents[] = $post_id;
$children = get_posts(
array(
\'post_type\' => \'page\'
, \'posts_per_page\' => -1
, \'post_parent\' => $parent_id
, \'order_by\' => \'title\'
, \'order\' => \'ASC\'
));
if (empty($children)) return;
echo \'<ul class="children level-\'.$current_level.\'-children">\';
foreach ($children as $child)
{
echo \'<li\';
if (in_array($child->ID, $top_parents))
{
echo \' class="current_page_item"\';
}
echo \'>\';
echo \'<a href="\'.get_permalink($child->ID).\'">\';
echo apply_filters(\'the_title\', $child->post_title);
echo \'</a>\';
// now call the same function for child of this child
if ($child->ID && (in_array($child->ID, $top_parents)))
{
show_all_children($child->ID, $post_id, $current_level+1);
}
echo \'</li>\';
}
echo \'</ul>\';
}
在中sidebar.php:<?php
$parents_ids = get_post_ancestors($post->ID);
$top_parent_id = (count($parents_ids) > 0) ? $parents_ids[count($parents_ids)-1] : $post->ID;
show_all_children($top_parent_id, $post->ID, 1);
?>
我应该将PHP代码放置在何处,以根据某些条件插入或不插入菜单项,例如另一个PHP变量的值,如下所示:$listItem = \'\'; <?php if ($condition) { $listItem = \'<li id=\"menu-item-44\" class=\"menu-item menu-item-type-taxonomy menu-item-object-category menu-item-44\"><a title=