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

时间:2019-06-19 19:56:21

标签: php wordpress

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

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

1 个答案:

答案 0 :(得分:0)

我对此玩得有些开心。我的结果可能与您要找的结果不完全相同,但我认为可能会更好。简而言之,下面的代码片段使您可以轻松识别当前页面的页面层次结构(父/子)。这样,您就可以按照自己的意愿做各种奇妙的事情。为了您的特定请求,我使自定义函数“ custom_list_child_pages(...)”从与“当前页面”相关的页面层次结构中返回链接的完整列表。

有很多逻辑,所有逻辑都与您要解决的关键情况有关。您应该能够确定自己何时位于子页面或父页面中,应该能够调出该小函数并对其进行修改,以使其能够与所有相关播放器(当前,父对象)一起执行[或者如果没有其他则为self],子页面等)。

让我知道这是否对您有帮助,以及您是否对代码或任何其他有关您要实现的目标的方面感到困惑,请让我也知道,我将为您提供进一步的帮助。

代码:

<?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.
}

?>

关于, 附庸风雅