如何在自定义侧边栏菜单上突出显示当前子页面?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (7)

我在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 } ?>

我假设它将在输出部分,但我根本不知道如何定位正在浏览的当前子页面并相应地突出显示。

提问于
用户回答回答于

我对这个有一点乐趣。我的结果可能不是你想要的,但我认为它可能会更好。简而言之,下面的代码段允许您轻松识别当前页面的页面层次结构(父/子)。因此,您可以根据自己的心愿做各种奇妙的事情。出于特定请求的目的,我使自定义函数“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.
}

?>

问候,Arty

扫码关注云+社区

领取腾讯云代金券