add class name to current menu item for childpages of parent item in menu for wordpress

时间:2017-08-04 12:14:24

标签: wordpress menu parent-child

i am using the following code to print a menu of a parent's child pages into a ul li menu. it works great. however i do not have ability to pipe in a classname just for the current page the user is on - same concept wordpress uses for the main menu code - i am curious what i would need to change in this code to pipe in a current menu item classname?

function nee_list_child_pages() {

    global $post;

    $string = '';

    if ( is_page() && $post->post_parent )

        $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
    else
        $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );

    if ( $childpages ) {
        $string = '<div id="sidebar_menu"><ul>' . $childpages . '</ul></div>';

        return $string;
    }
}

add_shortcode('wpb_childpages', 'nee_list_child_pages');

1 个答案:

答案 0 :(得分:0)

You have to create a NavWalker for this. Here a example implenentation: https://gist.github.com/kosinix/5544535

and the office docs: https://codex.wordpress.org/Class_Reference/Walker_Nav_Menu