[英]Create sidebar menu with subpages, from existing string using PHP
我想創建一個側邊欄菜單。 我在變量中有完整的網站菜單。 現在,我想收集當前頁面的子頁面/父頁面。
為了我的幫助,我開設了“祖先”和“當前”課程。
請在下面輸入我的代碼。 我需要Q1,Q2,Q3和Q4的幫助。 如果難以理解,請發表評論,我會更好地解釋。
<?php
// this is the menu for the complete site
$menu = '
<ul>
<li class="ancestor">page1
<ul class="child">
<li>page11</li>
<li class="parent">page12
<ul class="child">
<li>page111</li>
<li class="current">page112</li>
<li>page113</li>
<li>page114</li>
</ul>
</li>
<li>page13</li>
<li>page14</li>
</ul>
</li>
<li>page2</li>
<li>page3</li>
<li>page4</li>
</ul>
';
// I guess that DOM document is the right metodh for the job?
$doc = new DOMDocument();
$doc->loadHTML($menu);
$doc->removeChild($doc->firstChild); // remove doctype
$doc->replaceChild($doc->firstChild->firstChild->firstChild, $doc->firstChild); // remove <html><body></body></html>
// START
if ('Q1: check to see if li with class "current" has subpages or parent pages. With other words, determine if there shall be a sidebar menu or not')
{
if ('Q2: If there is a li with class "ancestor", then get all the children of this class')
{
}
elseif ('Q3: if there is no li with class "ancestor", then get all the children of "current" instead')
{
}
// Q4 remove the first ul
$wanted_li_s = $doc->saveHTML();
$sidebar = '<ul class="new-class">'.$wanted_li_s.'</ul>';
} // END if has subpages
else
{
$sidebar = ''; // if there are no subpages sidebar menu should return nothing
}
?>
在上面的示例中,我希望$ sidebar與以下相同:
<ul class="new-class">
<li>page11</li>
<li class="parent">page12
<ul class="child">
<li>page111</li>
<li class="parent">page112</li>
<li>page113</li>
<li>page114</li>
</ul>
</li>
<li>page13</li>
<li>page14</li>
</ul>
通過這種方法,您可以在幾乎不使用任何CMS的情況下創建簡單的子菜單(無需學習CMS語言)。 您要做的就是將HTML放入變量中,並找出檢測子頁面的方法。
因此,您需要包括此http://simplehtmldom.sourceforge.net/ (想像PHP的jQuery,您不會后悔的)
這是為wordpress自定義菜單創建子菜單的示例。
include('simple_html_dom.php');
$meny = wp_nav_menu( array(
'fallback_cb' => '' ,
'echo' => 0,
'container' => false,
'container' => '',
'items_wrap' => '%3$s'
));
$menu_string = str_get_html($meny);
foreach($menu_string->find('li[class=current-menu-item]') as $foreach_current_menu_item) // for each current-menu-item (only one of this)
{
if (strpos($foreach_current_menu_item->parent()->class,'sub-menu') !== false) // if you are on a subpage
{
foreach($menu_string->find('li[class=current-menu-ancestor]') as $foreach_current_menu_item2) //
{
$sidebar_menu= $foreach_current_menu_item2;
}
}
elseif (strpos($foreach_current_menu_item->last_child()->class,'sub-menu') !== false) // if you are on a head page WITH subpages
{
$sidebar_menu= $foreach_current_menu_item;
}
else // if you are on a head page WITHOUT subpages
{
$sidebar_menu = '';
}
}
// echo the menu where ever you want in your page
echo '<ul id="new-id-for-the-menu">'.sidebar_menu.'</ul>';
要使其在側邊欄菜單中的第3級頁面上工作,可以使用:
if (strpos($foreach_current_menu_item->parent()->class,'sub-menu') !== false)
{
$i = 0;
foreach($menu_string->find('li[class=current-menu-ancestor]') as $foreach_current_menu_ancestor)
{
if ($i == 0)
{
$sidomeny = $foreach_current_menu_ancestor;
}
$i++;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.