【问题标题】:How to highlight current child page on custom sidebar menu?如何在自定义侧边栏菜单上突出显示当前子页面?
【发布时间】:2019-06-20 08:17:53
【问题描述】:

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

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

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我玩得有点开心。我的结果可能不是你想要的,但我认为它可能会更好。简而言之,下面的 sn-p 允许您轻松识别当前页面的页面层次结构(父/子)。这样,你就可以随心所欲地做各种美妙的事情。为了满足您的特定要求,我使自定义函数“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.
    }
    
    ?>
    

    问候, 附庸风雅

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      • 1970-01-01
      • 2022-08-09
      • 1970-01-01
      • 2016-02-24
      相关资源
      最近更新 更多