我做这件事的另一种方法是直接抓取帖子,而不是使用wp\\u nav\\u菜单。这是基于实际的页面结构,而不是菜单。
功能。php:
function __construct()
{
$this->currentPageID = $this->getCurrentPageID();
$this->sectionChildren = $this->getSectionChildren();
}
function getCurrentPageID()
{
$currentPage = $_SERVER[\'REQUEST_URI\'];
if($currentPage == \'/\')
$currentPage = \'/home\';
$currentPage = get_page_by_path($currentPage);
if($currentPage)
return $currentPage->ID;
else
return -1;
}
function getSectionID()
{
global $wpdb;
$currentSectionID = $wpdb->get_var("
SELECT post_parent
FROM ". $wpdb->posts ."
WHERE ID = ". $this->currentPageID
);
if($currentSectionID == 0)
return $this->currentPageID;
else
return $currentSectionID;
}
function getSectionChildren()
{
global $wpdb;
$children = $wpdb->get_results("
SELECT ID, post_title
FROM ". $wpdb->posts ."
WHERE
post_parent = ". $this->getSectionID() ." AND
post_type = \'page\' AND
post_status = \'publish\'
", ARRAY_A);
return $children;
}
侧栏。php:
<ul id="sub-navigation">
<?php foreach($dc->sectionChildren as $c) : ?>
<li <?php if($dc->currentPageID == $c[\'ID\']) echo \'class="active"\'; ?>><a href="<?php echo get_permalink($c[\'ID\']); ?>"><?php echo $c[\'post_title\']; ?></a></li>
<?php endforeach; ?>
</ul>