Make wp_list_pages print slugs instead of titles

By default wp_list_pages spits out an unordered list of post titles. To save space in a sidebar menu I´m trying to create a list of page slugs instead of page titles.

I suppose I´ll need to write a custom walker.. I was hoping someone can help me with an easier solution or a code snippet.

Related posts

Leave a Reply

1 comment

  1. I would recommend using get_pages() rather than wp_list_pages() for this purpose.

    The get_pages() function will return an array of objects of Pages, and you can use the array data to build your own HTML list. e.g.:

    <?php
    // Wrap in a function, for later output;
    function wpse47989_list_pages() {
        // Get an array of pages using default arguments
        // See Codex for list of arguments
        $wpse47989_pages = get_pages();
    
        // Open the unordered list
        $page_list = '<ul>';
    
        // Loop through the results, and output an HTML list
        foreach ( $wpse47989_pages as $wpse47989_page ) {
            // Open the list item
            $page_list .= '<li>';
            // Open the link
            $page_list .= '<a href="' . $wpse47989->permalink . '">';
            // Link anchor text (post slug)
            $page_list .= $wpse47989_page->post_name;
            // Close the link
            $page_list .= '</a>';
            // Close the list item
            $page_list .= '</li>';
        }
    
        // Close the unordered list
        $page_list .= '</ul>';
    }
    ?>
    

    Then, of course, you just output the function wherever you want to display the list. (Note: this would be a good candidate for a custom Widget. You could even pass the function get_pages() arguments as Widget options.)

    You may need to manipulate the foreach output for styling/etc.