我在一个WordPress站点上有一个侧边栏菜单,可以简单地输出该父站点下的所有子页面。我正在尝试突出显示(或者希望添加一个箭头)当前选择的子页面。由于我有限的PHP经验,我遇到了困难,无法弄清楚如何做到这一点。
任何帮助都将不胜感激。相关代码如下:
<?php
/* if the current pages has a parent, i.e. we are on a subpage */
if($post->post_parent){
/* $children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); // list the parent page */
$children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable
}
/* else if the current page does not have a parent, i.e. this is a top level page */
else {
//$children = wp_list_pages("title_li=&include=".$post->ID."&echo=0"); // include the parent page as well
$children .= wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&"); // form a list of the children of the current page
}
/* if we ended up with any pages from the queries above */
if ($children) { ?>
<ul class="submenu">
<?php echo $children; /*print list of pages*/ ?>
</ul>
<?php } ?>
我假设它应该在输出部分,但我只是不知道如何定位正在浏览的当前子页面并相应地突出显示它。
发布于 2019-06-20 08:17:53
我在这本书上玩得很开心。我的结果可能不完全是你想要的,但我认为它可能会更好。简而言之,下面的代码片段允许您轻松地识别当前页面的页面层次结构(父/子页面)。如此一来,你就可以随心所欲地做各种奇妙的事情。为了满足您的特定请求,我创建了自定义函数"custom_list_child_pages(...)“返回页面层次结构中与“当前页面”相关的链接的完整列表。
有大量的逻辑,所有这些都与你试图解决的关键情况有关。当你在一个子页面或父页面时,你应该能够识别,你应该能够调用这个小函数并修改它,让它对所有相关的玩家(当前、父或自我,如果没有其他的话,子页面,等等)做任何你需要做的事情。
让我知道这是否对你有帮助,如果你卡在代码或任何其他方面,关于你试图实现的,让我也知道,我会看看我如何能进一步帮助你。
代码:
<?php
//die(var_dump($foo)); // Quickly check a variable value.
function custom_list_child_pages($the_current_page_id, $the_parent_page_id, $child_pages){
$html_page_listing = '';
// Get creative with the parent page.
$the_parent_page = get_page( $the_parent_page_id );
$html_page_listing .= "<h3>" . "<a href='$the_parent_page->guid'>$the_parent_page->post_title</a>" . "</h3>";
// Get creative with the child pages.
$html_page_listing .= "<h3>" . "Child pages:" . "</h3>";
$html_page_listing .= "<ul>";
// Iterate through child pages as desired.
foreach ($child_pages as $key => $page) {
$current_page = get_page_by_title( $page->post_title );
$current_page_id = $page->ID;
if ($current_page_id == $the_current_page_id) {
// Do something great.
$html_page_listing .= "<li> ->" . "<a href='$page->guid'>$page->post_title</a>" . "</li>";
} else {
$html_page_listing .= "<li>" . "<a href='$page->guid'>$page->post_title</a>" . "</li>";
}
}
$html_page_listing .= "</ul>";
return $html_page_listing;
}
global $post; // If outside the loop.
// Set up the objects needed.
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => '-1'));
if ( is_page() && $post->post_parent ) {
// This is a subpage.
$the_current_page_id = $page_id;
$the_parent_page_id = wp_get_post_parent_id( $post_ID );
$the_child_pages = get_page_children( $the_parent_page_id, $all_wp_pages ); // Form a list of the children of the current page.
$the_parent_page_id = wp_get_post_parent_id( $post_ID ); // Include the parent page as well.
$menu = custom_list_child_pages($the_current_page_id, $the_parent_page_id, $the_child_pages); // ~append the list of children pages to the same $children variable
echo $menu; // Print list of pages.
} else {
// This is not a subpage.
$the_current_page_id = $page_id;
$the_parent_page_id = $page_id;
$the_child_pages = get_page_children( $page_id, $all_wp_pages ); // Form a list of the children of the current page.
$the_parent_page_id = $page_id; // Include the parent page as well.
$menu = custom_list_child_pages($the_current_page_id, $the_parent_page_id, $the_child_pages);
echo $menu; // Print list of pages.
}
?>
问候你,阿蒂
https://stackoverflow.com/questions/56674986
复制相似问题